问题
I am taking an image from the camera, and rotating it to a new orientation, and then pasting it onto the general pasteboard. However, no matter what I pass in to orientation, what ends up being pasted later by the pasteboard paste command is oriented as if it were 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];
I use similar code to successfully rotate images from the camera and insert them into the photo album:
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];
How come I can't get the image rotated as I wish for the pasteboard? Is the paste action using an EXIF orientation? If so, is imageWithCGImage copying the original EXIF to the new image?
回答1:
I believe the pasteboard prefers PNG, which would strip any EXIF data attached to the image.
NSLog( @"UIPasteboardTypeListImage: %@", UIPasteboardTypeListImage );
nets:
UIPasteboardTypeListImage: (
"public.png",
"public.tiff",
"public.jpeg",
"com.compuserve.gif"
)
Options for work-arounds would be to save JPEG data to the pasteboard:
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];
or reset the orientation of the image after fetching it from the pasteboard:
[[UIPasteboard generalPasteboard] setImage:img];
...
UIImage *pbImg = [[UIPasteboard generalPasteboard] image];
UIImage *correctedImage = [UIImage imageWithCGImage:[pbImg CGImage] scale:pbImg.scale orientation:__whateverOrientationDesired];
来源:https://stackoverflow.com/questions/9052852/why-does-the-uipasteboard-ignore-the-image-orientation-i-use