2012-09-30 63 views
3

下面的代码将图像拆分为2.它似乎与非视网膜设备正常工作,但它给视网膜设备不同的输出。有人能帮我解决吗?谢谢..奇怪的行为与CGContext - iOS

我的代码

UIImage *img = [UIImage imageNamed:@"apple.png"]; 

CGSize sz = [img size]; 
UIGraphicsBeginImageContextWithOptions(CGSizeMake(sz.width/2, sz.height), NO, 0); 
[img drawAtPoint:CGPointMake(-sz.width/2, 0)]; 
UIImage *right = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
rightView = [[[UIImageView alloc] initWithImage:right] autorelease]; 
rightView.frame = CGRectMake(self.view.frame.size.width/2, 0, self.view.frame.size.width/2, self.view.frame.size.height); 

CGImageRef leftRef = CGImageCreateWithImageInRect([img CGImage],CGRectMake(0,0,sz.width/2,sz.height)); 
UIGraphicsBeginImageContextWithOptions(CGSizeMake(sz.width/2, sz.height), NO, 0); 
CGContextRef con = UIGraphicsGetCurrentContext(); 
CGContextDrawImage(con, CGRectMake(0,0,sz.width/2.0,sz.height), leftRef); 
UIImage *left = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImage *rotatedImage = [left imageRotatedByDegrees:180.0]; 

leftView = [[[UIImageView alloc] initWithImage:rotatedImage] autorelease]; 
leftView.frame = CGRectMake(0, 0, self.view.frame.size.width/2, self.view.frame.size.height); 
leftView.transform = CGAffineTransformMake(-1,0,0,1,0,0); 

CGImageRelease(leftRef); 


[self.view addSubview:leftView]; 
[self.view addSubview:rightView]; 

非视网膜

enter image description here

视网膜

enter image description here

PS:我不知道这是重要的,但apple.png有一个@ 2x版本..

回答

2

[-UIImage size]属性以磅为单位返回大小,而不是以像素为单位。您可能还需要拨打[-UIImage scale]来了解如何缩放图像。

+0

左右图像的比例相同。 – user1526474

0

当您使用

leftView = [[[UIImageView alloc] initWithImage:rotatedImage] autorelease];, 

你没有指定正确的尺度左视图。而不是创建您的UIImage这样:

UIImage *left = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImage *rotatedImage = [left imageRotatedByDegrees:180.0]; 

尝试创建一个CGImageRef,然后用

[UIImage imageWithCGImage:scale:orientation:] 

,同时指定了正确的尺度初始化的UIImage。有许多方法可以将原始图像数据从上下文转换为CGImageRef,或者您可以使用您创建的图像并使用UIImage的CGImage属性。

+0

谢谢你的回答..你能举个例子吗? – user1526474