将NSData转换为CGImage然后再转换回NSData会使文件太大

Wil*_*ill 3 objective-c cgimage nsdata ios cgimageref

我用相机制作了相机AVFoundation.

一旦我AVCaptureStillImageOutput完成了它的captureStillImageAsynchronouslyFromConnection:completionHandler:方法,我就像这样创建一个NSData对象:

         NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
Run Code Online (Sandbox Code Playgroud)

一旦我有了这个NSData对象,我想将图像 - 不用 - 转换为a UIImage.我发现我可以转换成a CGImage来这样做.

在我拥有imageData之后,我开始转换为CGImage的过程,但我发现CGImageRef最终比NSData对象大三十倍.

下面是我用来转换为代码CGImage来自NSData:

CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)(imageData));
CGImageRef imageRef = CGImageCreateWithJPEGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);
Run Code Online (Sandbox Code Playgroud)

如果我尝试NSLog超出图像的大小,当它NSData是一个1.5-2兆字节的图像时,它会达到30兆字节!

size_t imageSize = CGImageGetBytesPerRow(imageRef) * CGImageGetHeight(imageRef);

    NSLog(@"cgimage size = %zu",imageSize);
Run Code Online (Sandbox Code Playgroud)

我想也许当你从NSData转到CGImage时,图像会解压缩,然后如果我转换回NSData,它可能会回到正确的文件大小.

imageData = (NSData *) CFBridgingRelease(CGDataProviderCopyData(CGImageGetDataProvider(imageRef)));
Run Code Online (Sandbox Code Playgroud)

以上与对象NSData相同.lengthCGImageRef

如果我尝试保存图像,则图像是无法打开的30mb图像.

我对使用CGImage完全不熟悉,所以我不确定我是从NSData转换为CGImage还是错误地返回,或者我是否需要调用某种方法再次解压缩.

提前致谢,

mik*_*eho 6

我正在做一些图像处理,并在SO上遇到了你的问题.似乎没有其他人想出答案,所以这是我的理论.

虽然理论上可以按照你描述的方式转换CGImageRef回来NSData,但数据本身是无效的,而不是真正的JPEGPNG,因为你发现它不可读.所以我不认为这NSData.length是正确的.您必须跳过许多步骤来重新创建一个NSData表示CGImageRef:

// incoming image data
NSData *image;

// create the image ref
CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef) image);
CGImageRef imageRef = CGImageCreateWithJPEGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);

// image metadata properties (EXIF, GPS, TIFF, etc)
NSDictionary *properties;

// create the new output data
CFMutableDataRef newImageData = CFDataCreateMutable(NULL, 0);
// my code assumes JPEG type since the input is from the iOS device camera
CFStringRef type = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, (__bridge CFStringRef) @"image/jpg", kUTTypeImage);
// create the destination
CGImageDestinationRef destination = CGImageDestinationCreateWithData(newImageData, type, 1, NULL);
// add the image to the destination
CGImageDestinationAddImage(destination, imageRef, (__bridge CFDictionaryRef) properties);
// finalize the write
CGImageDestinationFinalize(destination);

// memory cleanup
CGDataProviderRelease(imgDataProvider);
CGImageRelease(imageRef);
CFRelease(type);
CFRelease(destination);

NSData *newImage = (__bridge_transfer NSData *)newImageData;
Run Code Online (Sandbox Code Playgroud)

通过这些步骤,newImage.length应该是相同的image.length.我没有测试,因为我实际上在输入和输出之间进行裁剪,但基于裁剪,大小大致与我的预期相同(输出大约是输入像素的一半,因此输出长度大约是一半大小)输入长度).