访问UIImage属性而不在内存中加载图像

kod*_*oda 18 iphone size image loading uiimage

如您所知,iphone指南不鼓励加载大于1024x1024的uiimages.

我必须加载的图像大小各不相同,我想检查我即将加载的图像的大小; 但是使用uiimage的.size属性需要对图像进行加密...这正是我想要避免的.

我的推理是否有问题或有解决方法吗?

谢谢你们

Ole*_*ann 33

从iOS 4.0开始,iOS SDK包含CGImageSource...功能(在ImageIO框架中).它是一种非常灵活的API,用于查询元数据而无需将图像加载到内存中.获取图像的像素尺寸应该像这样工作(确保在目标中包含ImageIO.framework):

#import <ImageIO/ImageIO.h>

NSURL *imageFileURL = [NSURL fileURLWithPath:...];
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)imageFileURL, NULL);
if (imageSource == NULL) {
    // Error loading image
    ...
    return;
}

CGFloat width = 0.0f, height = 0.0f;
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);

CFRelease(imageSource);

if (imageProperties != NULL) {

    CFNumberRef widthNum  = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);
    if (widthNum != NULL) {
        CFNumberGetValue(widthNum, kCFNumberCGFloatType, &width);
    }

    CFNumberRef heightNum = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
    if (heightNum != NULL) {
        CFNumberGetValue(heightNum, kCFNumberCGFloatType, &height);
    }

    // Check orientation and flip size if required
    CFNumberRef orientationNum = CFDictionaryGetValue(imageProperties, kCGImagePropertyOrientation);
    if (orientationNum != NULL) {
        int orientation;
        CFNumberGetValue(orientationNum, kCFNumberIntType, &orientation);
        if (orientation > 4) {
            CGFloat temp = width;
            width = height;
            height = temp;
        }
    }

    CFRelease(imageProperties);
}

NSLog(@"Image dimensions: %.0f x %.0f px", width, height);
Run Code Online (Sandbox Code Playgroud)

(改编自Gelphman和Laden的"Programming with Quartz",第9.5页,第228页)

  • @OleBegemann还应考虑图像方向(`kCGImagePropertyOrientation`) - 方向5到8的翻转宽度和高度.您可以使用[这些图像]进行测试(https://github.com/recurser/exif-orientation-examples) . (4认同)
  • 在调用CFNumberGetValue()时传递`kCFNumberCGFloatType`而不是`kCFNumberFloatType`非常重要,因为变量`width`和`height`被声明为`CGFloat`.上面的代码适用于32位系统,但这些值将包含64位系统上的垃圾. (3认同)
  • 为什么使用`CGImageSourceCopyPropertiesAtIndex`而不仅仅是`CGImageSourceCopyProperties`? (2认同)

Ale*_* N. 6

Swift 3版本的答案:

import Foundation
import ImageIO

func sizeForImage(at url: URL) -> CGSize? {

    guard let imageSource = CGImageSourceCreateWithURL(url as CFURL, nil)
        , let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as? [AnyHashable: Any]
        , let pixelWidth = imageProperties[kCGImagePropertyPixelWidth as String]
        , let pixelHeight = imageProperties[kCGImagePropertyPixelHeight as String]
        , let orientationNumber = imageProperties[kCGImagePropertyOrientation as String]
        else {
            return nil
    }

    var width: CGFloat = 0, height: CGFloat = 0, orientation: Int = 0

    CFNumberGetValue(pixelWidth as! CFNumber, .cgFloatType, &width)
    CFNumberGetValue(pixelHeight as! CFNumber, .cgFloatType, &height)
    CFNumberGetValue(orientationNumber as! CFNumber, .intType, &orientation)

    // Check orientation and flip size if required
    if orientation > 4 { let temp = width; width = height; height = temp }

    return CGSize(width: width, height: height)
}
Run Code Online (Sandbox Code Playgroud)