访问图像属性而不将图像加载到内存中

有时您可能想要从图像文件中检索某些属性,例如图像的尺寸或其他元数据,而不会在屏幕上实际显示全尺寸图像。在iOS上最简单的方法是使用UIImage

    NSString *path = [[NSBundle mainBundle] pathForResource:@"KAR_5160" ofType:@"jpg"];

    UIImage *image = [UIImage imageWithContentsOfFile:path];
    NSLog(@"%@", NSStringFromCGSize(image.size));

这种方法的问题是整个图像被加载到内存中。而且由于像素数据未被压缩存储在存储器中,所以即使是512×512像素(填满不到iPhone 4屏幕的一半)也将占用1 MB的内存。

CGImageSource

从iOS 4开始,SDK包含了CGImageSource...一整套功能的更好的解决方案,这些功能永远都可以在Mac上使用。这些功能允许您访问某些图像元数据,而无需将实际像素数据加载到内存中。例如,获取像素尺寸的工作原理如下(确保在您的目标中包含ImageIO.framework):

#import 

NSString *path = [[NSBundle mainBundle] pathForResource:@"KAR_5160" ofType:@"jpg"];
//
//    UIImage *image = [UIImage imageWithContentsOfFile:path];
//    NSLog(@"%@", NSStringFromCGSize(image.size));
    
    
    NSURL *imageFileURL = [NSURL fileURLWithPath:path];
    CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)imageFileURL, NULL);
    if (imageSource == NULL) {
        // 图片加载失败
        return;
    }
    
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:NO], (NSString *)kCGImageSourceShouldCache,
                             nil];
    CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (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);
    
    
    // 获取想要的元数据
    // 返回的字典CGImageSourceCopyPropertiesAtIndex()包含的不仅仅是图像尺寸。如果存在,它包括完整的EXIF和IPTC元数据以及TIFF,GIF,JPEG,PNG和原始文件(以及其他)的各种文件格式特定信息。
    // 举一个例子,我们来看一下拍摄照片的日期,相机的型号名称和存储在元数据中的GPS坐标:

    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);
    }


你可能感兴趣的:(访问图像属性而不将图像加载到内存中)