2012-01-29 71 views
1

我正在拍攝相機中的圖像,並將其旋轉到新的方向,然後將其粘貼到一般粘貼板上。但是,無論我傳入方向,後來由pasteboard paste命令粘貼的內容都將定向爲UIImageOrientationUp。爲什麼UIPasteboard會忽略我使用的圖片方向?

CGImageRef croppedImageRef = CGImageCreateWithImageInRect([self CGImage], CGRectMake(rect.origin.y, [self size].width - rect.origin.x - rect.size.width, rect.size.height, rect.size.width)); 
    UIImage *croppedImage = [UIImage imageWithCGImage:croppedImageRef scale:self.scale orientation:orientation]; 
    CGImageRelease(croppedImageRef); 
    [[UIPasteboard generalPasteboard] setImage:croppedImage]; 

我用類似的代碼,以成功地從相機旋轉圖像,並將其插入到相冊:

CGImageRef croppedImageRef = CGImageCreateWithImageInRect([self CGImage], CGRectMake(rect.origin.y, [self size].width - rect.origin.x - rect.size.width, rect.size.height, rect.size.width)); 
    UIImage *croppedImage = [UIImage imageWithCGImage:croppedImageRef scale:self.scale orientation:orientation]; 
    CGImageRelease(croppedImageRef); 
    [self writeUIImageToCameraRoll:croppedImage withOrientation:orientation]; 

爲什麼我不能得到的圖像旋轉,我希望爲紙板?是否使用EXIF方向進行粘貼操作?如果是這樣,imageWithCGImage是否將原始EXIF複製到新圖像?

回答

0

我相信剪貼板更喜歡PNG,它會去除附加在圖像上的任何EXIF數據。

NSLog(@"UIPasteboardTypeListImage: %@", UIPasteboardTypeListImage);網:

的變通
UIPasteboardTypeListImage: (
    "public.png", 
    "public.tiff", 
    "public.jpeg", 
    "com.compuserve.gif" 
) 

選項將JPEG數據保存到剪貼板:

NSData *jpegData = UIImageJPEGRepresentation(img, 1.0); 
[[UIPasteboard generalPasteboard] setData:jpegData forPasteboardType:(id)kUTTypeJPEG]; 
... 
NSData *pbImgData = [[UIPasteboard generalPasteboard] dataForPasteboardType:(id)kUTTypeJPEG]; 
UIImage *pbImg = [UIImage imageWithData:pbImgData]; 

或從剪貼板取它復位後圖像的方向:

[[UIPasteboard generalPasteboard] setImage:img]; 
... 
UIImage *pbImg = [[UIPasteboard generalPasteboard] image]; 
UIImage *correctedImage = [UIImage imageWithCGImage:[pbImg CGImage] scale:pbImg.scale orientation:__whateverOrientationDesired]; 
相關問題