ben*_*sie 0 exif objective-c nsdata ios
在iOS应用中,我有一个NSData对象,它是一个JPEG文件,需要将其调整为给定的分辨率(2048x2048),并且需要将JPEG质量设置为75%。需要在将EXIF数据保留在文件中的同时进行设置。该照片不在相机胶卷中-它是从DSLR相机通过网络拉出的,只是与应用程序临时存储在一起。如果图像通过UIImage行程,则EXIF数据将丢失。如何在不丢失EXIF的情况下执行调整大小和设置质量?还是有一种方法可以在转换前剥离EXIF数据,并在完成后将其添加回去?
小智 5
您可以尝试CGImageSourceCreateThumbnailAtIndex和CGImageSourceCopyPropertiesAtIndex调整大小为jpeg图像的NSData对象的大小,而不会丢失EXIF。
我从问题设置图像的exif数据中得到启发
以下是我出于相同目的的代码。
干杯
+ (NSData *)JPEGRepresentationSavedMetadataWithImage:(NSData *)imageData compressionQuality:(CGFloat)compressionQuality maxSize:(CGFloat)maxSize
{
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
CFDictionaryRef options = (__bridge CFDictionaryRef)@{(id)kCGImageSourceCreateThumbnailWithTransform: (id)kCFBooleanTrue,
(id)kCGImageSourceCreateThumbnailFromImageIfAbsent: (id)kCFBooleanTrue,
(id)kCGImageSourceThumbnailMaxPixelSize: [NSNumber numberWithDouble: maxSize], // The maximum width and height in pixels of a thumbnail
(id)kCGImageDestinationLossyCompressionQuality: [NSNumber numberWithDouble:compressionQuality]};
CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(source, 0, options); // Create scaled image
CFStringRef UTI = kUTTypeJPEG;
NSMutableData *destData = [NSMutableData data];
CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)destData, UTI, 1, NULL);
if (!destination) {
NSLog(@"Failed to create image destination");
}
CGImageDestinationAddImage(destination, thumbnail, CGImageSourceCopyPropertiesAtIndex(source, 0, NULL)); // copy all metadata in source to destination
if (!CGImageDestinationFinalize(destination)) {
NSLog(@"Failed to create data from image destination");
}
CFRelease(destination);
CFRelease(source);
CFRelease(thumbnail);
return [destData copy];
}
Run Code Online (Sandbox Code Playgroud)