Why does the UIPasteboard ignore the image orientation I use?

限于喜欢 提交于 2019-12-11 11:32:49

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!