获取图片属性的步骤
获取图片属性的方法
很多时候我们需要获取一些图片的属性,例如图片的大小、元数据等。最简单的方法就是使用UIImage实现。
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"pic.JPG"]; UIImage *image = [UIImage imageWithContentsOfFile:path]; CGSize imageSize = image.size;
这种实现方式的问题就是必须完整的将图片载入到内存才能获取图片的属性。
从iOS 4开始,苹果的SDK提供了一个更好的解决方案,那就是CGImageSource(需要导入ImageIO.framework)。这种实现方式就不需要将图片载入到内存。
例如,获取图片的大小,使用CGImageSource的实现方式如下:
#import <ImageIO/ImageIO.h> NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"pic.JPG"]; NSURL *imageFileURL = [NSURL fileURLWithPath:path]; CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)imageFileURL, NULL); if (imageSource) { NSDictionary *options = @{(NSString *)kCGImageSourceShouldCache:@NO}; CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (__bridge CFDictionaryRef)options); if (imageProperties) { NSNumber *width = (NSNumber *)CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth); NSNumber *height = (NSNumber *)CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight); NSLog(@"Image dimensions: %@ x %@ px", width, height); CFRelease(imageProperties); } CFRelease(imageSource); } else { NSLog(@"Error loading image"); }
上面的例子中CGImageSourceCopyPropertiesAtIndex()返回的数据中包含了很多图片尺寸以外的信息,例如EXIF、IPTC等。
下面举一个例子,用来说明如何获取我们需要的特定属性。例如拍照日期、相机型号、GPS信息。
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"pic.JPG"]; NSURL *imageFileURL = [NSURL fileURLWithPath:path]; CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)imageFileURL, NULL); if (imageSource) { NSDictionary *options = @{(NSString *)kCGImageSourceShouldCache:@NO}; CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (__bridge CFDictionaryRef)options); if (imageProperties) { CFDictionaryRef exif = CFDictionaryGetValue(imageProperties, kCGImagePropertyExifDictionary); if (exif) { NSString *dateTakenString = (NSString *)CFDictionaryGetValue(exif, kCGImagePropertyExifDateTimeOriginal); NSLog(@"Date Taken: %@", dateTakenString); } CFDictionaryRef tiff = CFDictionaryGetValue(imageProperties, kCGImagePropertyTIFFDictionary); if (tiff) { NSString *cameraModel = (NSString *)CFDictionaryGetValue(tiff, kCGImagePropertyTIFFModel); NSLog(@"Camera Model: %@", cameraModel); } CFDictionaryRef gps = CFDictionaryGetValue(imageProperties, kCGImagePropertyGPSDictionary); if (gps) { NSString *latitudeString = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLatitude); NSString *latitudeRef = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLatitudeRef); NSString *longitudeString = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLongitude); NSString *longitudeRef = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLongitudeRef); NSLog(@"GPS Coordinates: %@ %@ / %@ %@", longitudeString, longitudeRef, latitudeString, latitudeRef); } CFRelease(imageProperties); } CFRelease(imageSource); } else { NSLog(@"Error loading image"); }