为什么UIPasteboard忽略我使用的图像方向?
我正在拍摄相机的图像,并将其旋转到新的方向,然后粘贴到一般的纸板上。 但是,无论我传入方向,粘贴板命令后来粘贴的方向都像是UIImageOrientationUp一样。
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到新的图像?
我相信剪贴板更喜欢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];