从包含 CGImage 的 UIImage 对象获取 NSData [英] Getting NSData from UIImage object that contains CGImage
问题描述
为了将图像文件上传到我的服务器,我需要获取它的 NSData.我现在无法执行此操作,因为我的 UIImage 包含一个 CGImage.
In order to upload an image file to my server, I need to get it's NSData. I am having trouble doing this right now, because my UIImage contains a CGImage.
让我向您展示我是如何做事的.当用户拍照时,这就是我对拍摄的照片所做的:
Let me show you how I am doing things. When a user takes a photo, this is what I do with the captured photo:
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [[UIImage alloc]initWithData:imageData];
_subLayer = [CALayer layer];
image = [self selfieCorrection:image];
image = [self rotate:image andOrientation:image.imageOrientation];
CGRect cropRectFinal = CGRectMake(cropRect.origin.x, 140, cropRect.size.width, cropRect.size.height);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRectFinal);
_subLayer.contents = (id)[UIImage imageWithCGImage:imageRef].CGImage;
在上面的代码中,我创建了一个 UIImage,并用 imageData
对象初始化它.我还创建了一个名为 _subLayer
的图层对象,它将在屏幕上显示最终图像.然后旋转和校正图像的方向,然后我设置一个 CGRect 来裁剪我想要保留的图像部分.
In the above code, I create a UIImage, and initialize it with the imageData
object. I also create a layer object called _subLayer
that will display the final image on the screen. The image's orientation is then rotated and corrected, and then I setup a CGRect to crop the part of the image I want to keep.
最重要的部分是最后 2 条语句.我使用原始 UIImage 创建了一个名为 imageRef
的 CGImageRef.然后,在最后一个语句中,我将 _subLayer
的内容属性设置为等于我的最终图像.
The most important part are the last 2 statements. I create a CGImageRef called imageRef
using the original UIImage. Then, in the last statement, I set the contents property of _subLayer
to equal my final image.
在同一个视图控制器的更深处,在 IBAction 内部,我有以下语句可以帮助我将图像传递给我的下一个视图控制器:sendFriendsVC.finalPhotoToSend = _subLayer.contents;
A little further down in this same view controller, inside of an IBAction I have the following statement which helps me pass the image to my next view controller: sendFriendsVC.finalPhotoToSend = _subLayer.contents;
finalPhotoToSend
是我在下一个视图控制器的头文件中设置的属性,如下所示:
finalPhotoToSend
is a property that I have setup in the header file of my next view controller like this:
@property (nonatomic, retain) UIImage *finalPhotoToSend;
当我转到下一个视图控制器时,数据成功传递,但是当我使用 NSLog finalPhotoToSend
时,即使它是一个 UIImage,它也会这样打印到控制台:
The data is successfully passed when I segue to the next view controller, but when I NSLog finalPhotoToSend
, even though it is a UIImage, it prints as this to the console:
<CGImage 0x1563dac0>
为了将照片上传到我的后端服务器服务,我需要使用 NSData 创建一个对象.我尝试使用这 2 种方法从 finalPhotoToSend
对象中获取 NSData:
In order to upload photos to my backend server service, it requires me to create an object using NSData. I have tried using these 2 methods to get the NSData out of the finalPhotoToSend
object:
NSData *imageData = UIImageJPEGRepresentation(finalPhotoToSend, 0.7);
和
NSData *imageData = UIImagePNGRepresentation(finalPhotoToSend);
但那些总是在 xcode 中给我以下错误:
But those always give me the following error in xcode:
NSInvalidArgumentException', reason: '-[__NSCFType CGImage]: unrecognized selector sent to instance
我不知道该怎么做.我应该使用不同的方法从 UIImage 中获取 NSData,因为它在技术上持有 CGImage?在将数据传递给下一个视图控制器之前,我是否应该做一些不同的事情?
I am not sure what to do. Is there a different method I should be using to get NSData out of my UIImage since it is technically holding a CGImage? Should I be doing something differently before I even pass the data to my next view controller?
为迈克尔
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:self {
if ([segue.identifier isEqualToString:@"mediaCaptureToSendToFriendsSegue"]) {
SendToFriendsViewController *sendFriendsVC = segue.destinationViewController;
sendFriendsVC.finalPhotoToSend = _subLayer.contents;
}
}
推荐答案
更好的答案:
您需要真正将 UIImage 对象(而不是 CGImage 指针)设置为您已声明为 UIImage 对象的属性.
You need to truly set a UIImage object (and not a CGImage pointer) to a property that you've declared to be a UIImage object.
原文:
如果finalPhotoToSend
"是一个属性,你应该这样做:
if "finalPhotoToSend
" is a property, you should be doing:
NSData *imageData = UIImageJPEGRepresentation(self.finalPhotoToSend, 0.7);
而不是:
NSData *imageData = UIImageJPEGRepresentation(finalPhotoToSend, 0.7);
这篇关于从包含 CGImage 的 UIImage 对象获取 NSData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!