【OC】CGImage获取图像分辨率

+ (NSDictionary *)imagePropertyWithPath:(NSString *)imagePath {
    if (!([[NSFileManager defaultManager] fileExistsAtPath:imagePath])) {
        return nil;
    }
    
    NSURL *imageFileURL = [NSURL fileURLWithPath:imagePath];
    CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)imageFileURL, NULL);
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:NO], (NSString *)kCGImageSourceShouldCache, nil];
    CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (CFDictionaryRef)options);
    CFRelease(imageSource);
    if (imageProperties) {
        NSDictionary *treeDict = [NSDictionary dictionaryWithDictionary:(NSDictionary*)(imageProperties)];
        return treeDict;
    }
    return @{};
}

注意测试的时候,关闭沙盒。不然可能获取不到数据。

参考:
Get exif data in mac OS development
nsimage-exif-metadata.html

你可能感兴趣的:(xcode,objective-c)