为什么 UIPasteboard 忽略我使用的图像方向?

发布于 2024-12-31 23:58:45 字数 1114 浏览 4 评论 0原文

我正在从相机中拍摄图像,并将其旋转到新的方向,然后将其粘贴到通用粘贴板上。但是,无论我向方向传递什么内容,稍后通过粘贴板粘贴命令粘贴的内容都会被定向,就好像它是 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复制到新图像?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情稳全场 2025-01-07 23:58:45

我相信粘贴板更喜欢 PNG,它会去除附加到图像的任何 EXIF 数据。

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

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];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文