为什么UIPasteboard会忽略我使用的图像方向? [英] Why does the UIPasteboard ignore the image orientation I use?

查看:101
本文介绍了为什么UIPasteboard会忽略我使用的图像方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从相机拍摄图像,并将其旋转到新的方向,然后将其粘贴到普通粘贴板上.但是,无论我要输入什么方向,粘贴板粘贴命令最终粘贴的内容都会像UIImageOrientationUp一样被定向.

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

为什么我不能像我希望的那样旋转图像?粘贴动作是否使用EXIF方向?如果是这样,imageWithCGImage是否会将原始EXIF复制到新图像?

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?

推荐答案

我相信粘贴板更喜欢PNG,因为PNG会删除附加到图像的EXIF数据.

I believe the pasteboard prefers PNG, which would strip any EXIF data attached to the image.

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

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

解决方法的选项是将JPEG数据保存到粘贴板:

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

这篇关于为什么UIPasteboard会忽略我使用的图像方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆