如何制作从 UIImagePickerController 返回的 UIImage 的精确副本?

How do I make an exact copy of a UIImage returned from a UIImagePickerController?(如何制作从 UIImagePickerController 返回的 UIImage 的精确副本?)

本文介绍了如何制作从 UIImagePickerController 返回的 UIImage 的精确副本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 iPhone 相机返回的图像有不同的需求.我的应用会缩小图像以供上传和显示,最近,我添加了将图像保存到照片应用的功能.

I have divergent needs for the image returned from the iPhone camera. My app scales the image down for upload and display and, recently, I added the ability to save the image to the Photos app.

起初我将返回的值分配给两个单独的变量,但结果发现它们共享同一个对象,所以我得到了两个按比例缩小的图像,而不是一个全尺寸的图像.

At first I was assigning the returned value to two separate variables, but it turned out that they were sharing the same object, so I was getting two scaled-down images instead of having one at full scale.

在确定您不能执行 UIImage *copyImage = [myImage copy]; 后,我使用 imageWithCGImage 制作了一个副本,如下所示.不幸的是,这不起作用,因为副本(此处为 croppedImage)最终会从原件旋转 90º.

After figuring out that you can't do UIImage *copyImage = [myImage copy];, I made a copy using imageWithCGImage, per below. Unfortunately, this doesn't work because the copy (here croppedImage) ends up rotated 90º from the original.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{   
    // Resize, crop, and correct orientation issues
    self.originalImage = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
    UIImageWriteToSavedPhotosAlbum(originalImage, nil, nil, nil);
    UIImage *smallImage = [UIImage imageWithCGImage:[originalImage CGImage]]; // UIImage doesn't conform to NSCopy

    // This method is from a category on UIImage based on this discussion:
    // http://discussions.apple.com/message.jspa?messageID=7276709
    // It doesn't rotate smallImage, though: while imageWithCGImage returns
    // a rotated CGImage, the UIImageOrientation remains at UIImageOrientationUp!
    UIImage *fixedImage = [smallImage scaleAndRotateImageFromImagePickerWithLongestSide:480];
    ...
}

有没有办法复制 UIImagePickerControllerOriginalImage 图像而不在过程中修改它?

Is there a way to copy the UIImagePickerControllerOriginalImage image without modifying it in the process?

推荐答案

复制后台数据并旋转

这个问题以稍微不同的方式提出了一个关于 UIImage 的常见问题.本质上,您有两个相关的问题 - 深度复制和旋转.UIImage 只是一个容器,并具有用于显示的方向属性.UIImage 可以将其支持数据包含为 CGImageCIImage,但最常见的是 CGImage.CGImage 是一个包含指向底层数据的指针的信息结构,如果您阅读文档,复制该结构不会复制数据.所以...

Copy backing data and rotate it

This question asks a common question about UIImage in a slightly different way. Essentially, you have two related problems - deep copying and rotation. A UIImage is just a container and has an orientation property that is used for display. A UIImage can contain its backing data as a CGImage or CIImage, but most often as a CGImage. The CGImage is a struct of information that includes a pointer to the underlying data and if you read the docs, copying the struct does not copy the data. So...

正如我将在下一段中介绍的那样,深度复制数据将使图像旋转,因为图像在基础数据中旋转.

As I'll get to in the next paragraph deep copying the data will leave the image rotated because the image is rotated in the underlying data.

UIImage *newImage = [UIImage imageWithData:UIImagePNGRepresentation(oldImage)];

这将复制数据,但需要在将其交给 UIImageView 之类的东西之前设置方向属性以正确显示.

This will copy the data but will require setting the orientation property before handing it to something like UIImageView for proper display.

另一种深度复制的方法是绘制到上下文中并获取结果.假设是斑马.

Another way to deep copy would be to draw into the context and grab the result. Assume a zebra.

UIGraphicsBeginImageContext(zebra!.size)
zebra!.drawInRect(CGRectMake(0, 0, zebra!.size.width, zebra!.size.height))
let copy = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

深拷贝和旋转

旋转 CGImage 已得到解答.碰巧这个旋转的图像是一个新的CGImage,可以用来创建一个UIImage.

这篇关于如何制作从 UIImagePickerController 返回的 UIImage 的精确副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何制作从 UIImagePickerController 返回的 UIImage 的精确副本?

基础教程推荐