不使用 UIImagePngrepresentation 或 UIImageJpegRepresentation 将 UIImage 转换为 NSData [英] Convert UIImage to NSData without using UIImagePngrepresentation or UIImageJpegRepresentation

查看:32
本文介绍了不使用 UIImagePngrepresentation 或 UIImageJpegRepresentation 将 UIImage 转换为 NSData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 UIImage 转换为 NSData 但不使用 UIImagePngRepresentationUIImageJpegRepresentation,对于 photolib i 中的图像可以使用这里提到的 assetlib 方法 使用 ALAssetsLibrary 和 ALAsset 取出图像作为 NSData ,但对于捕获的图像,资产 url 不存在,因此在这种情况下,我需要将 UIImage 直接转换为带有 exif 数据的字节,我该如何实现?请帮忙

I Need to convert UIImage to NSData but without using UIImagePngRepresentation or UIImageJpegRepresentation, for images from photolib i can use assetlib method as mentioned here Using ALAssetsLibrary and ALAsset take out Image as NSData , but for captured image , assset url is not there hence in that case i need to convert UIImage directly to bytes with exif data , how can i accomplish this ? please help

推荐答案

H Bastan,

根据您的评论:

...我想通过UIImagepicker委托方法在文档目录中保存设备相机返回的捕获图像...

...I want to save the captured image return by device camera, through UIImagepicker delegate method in document directory...

看起来您的真正目标是将带有 EXIF 数据的图像保存到文档目录中,而不是专门将 UIImage 作为带有 EXIF 数据的 NSData 对象获取.在这种情况下,您可以在 imagePickerController:didFinishPickingMediaWithInfo:

It looks like your real goal is to save the image with EXIF data to the documents directory and not specifically getting the UIImage as an NSData object with the EXIF data. In that case, you can do the following in your implementation of imagePickerController:didFinishPickingMediaWithInfo:

// Get your image.
UIImage *capturedImage = [info objectForKey:UIImagePickerControllerOriginalImage];

// Get your metadata (includes the EXIF data).
NSDictionary *metadata = [info objectForKey:UIImagePickerControllerMediaMetadata];

// Create your file URL.
NSFileManager *defaultManager = [NSFileManager defaultManager];
NSURL *docsURL = [[defaultManager URLsForDirectory:NSDocumentDirectory
                                         inDomains:NSUserDomainMask] lastObject];
NSURL *outputURL = [docsURL URLByAppendingPathComponent:@"imageWithEXIFData.jpg"];

// Set your compression quuality (0.0 to 1.0).
NSMutableDictionary *mutableMetadata = [metadata mutableCopy];
[mutableMetadata setObject:@(1.0) forKey:(__bridge NSString *)kCGImageDestinationLossyCompressionQuality];

// Create an image destination.
CGImageDestinationRef imageDestination = CGImageDestinationCreateWithURL((__bridge CFURLRef)outputURL, kUTTypeJPEG , 1, NULL);
if (imageDestination == NULL ) {

    // Handle failure.
    NSLog(@"Error -> failed to create image destination.");
    return;
}

// Add your image to the destination.
CGImageDestinationAddImage(imageDestination, capturedImage.CGImage, (__bridge CFDictionaryRef)mutableMetadata);

// Finalize the destination.
if (CGImageDestinationFinalize(imageDestination) == NO) {

    // Handle failure.
    NSLog(@"Error -> failed to finalize the image.");
}

CFRelease(imageDestination);

根据我的测试,执行时间与执行时间大致相同或更快:

From my tests the execution time was about the same or faster than doing:

NSData *data = UIImageJPEGRepresentation(capturedImage, 1.0);
[data writeToURL:outputURL atomically:YES];

...你可以保留 EXIF 数据!

...and you get to keep the EXIF data!

如果您想确保您的 UI 保持响应,您也可以将其分派到后台队列:

You could also dispatch it to a background queue if you want to make sure your UI stays responsive:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

    // Do the image saving here...
});

重要说明:您需要将 ImageIO.frameworkMobileCoreServices.framework 添加到目标的构建阶段并将它们导入您保存图像的类:

Important Note: You will need to add the ImageIO.framework and the MobileCoreServices.framework to your target's build phases and import them in the class where you do the image saving:

#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>

其他注意事项我的理解是,在这种情况下,我们不会按照示例中的建议保存图像时产生代损失.我们正在使用 UIImage 的 CGImage 属性,如文档所述:

Other notes It is my understanding that in this case we're not creating a generation loss when saving the image as proposed in the example. We are using UIImage's CGImage property and as the documentation states:

底层 Quartz 图像数据

The underlying Quartz image data

这篇关于不使用 UIImagePngrepresentation 或 UIImageJpegRepresentation 将 UIImage 转换为 NSData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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