2012-09-30 59 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