访问UIImage属性而不在内存中加载图像 [英] accessing UIImage properties without loading in memory the image

查看:95
本文介绍了访问UIImage属性而不在内存中加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如您所知,iphone指南不鼓励加载大于1024x1024的图像。

As you know the iphone guidelines discourage loading uiimages that are greater than 1024x1024.

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

The size of the images that i would have to load varies, and i would like to check the size of the image i am about to load; however using the .size property of uiimage requires the image to be laoded... which is exactly what i am trying to avoid.

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

Is there something wrong in my reasoning or is there a solution to that?

谢谢大家

推荐答案

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

As of iOS 4.0, the iOS SDK includes the CGImageSource... functions (in the ImageIO framework). It's a very flexible API to query metadata without loading the image into memory. Getting the pixel dimensions of an image should work like this (make sure to include the ImageIO.framework in your target):

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

(改编自Gelphman和Laden的Quartz编程,第9.5页,第228页)

(adapted from "Programming with Quartz" by Gelphman and Laden, listing 9.5, page 228)

这篇关于访问UIImage属性而不在内存中加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆