如您所知,iphone指南不鼓励加载大于1024x1024的图像。
我必须加载的图像大小各不相同,我想检查我即将加载的图像的大小;但是使用uiimage的.size属性需要对图像进行加密...这正是我想要避免的。
我的推理是否有问题或是否有解决方法?
谢谢大家
答案 0 :(得分:33)
从iOS 4.0开始,iOS SDK包含CGImageSource...
functions(在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);
(改编自Gelphman和Laden的“Quartz编程”,第9.5页,第228页)
答案 1 :(得分:3)
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)
}
答案 2 :(得分:0)
在 Swift 5 中,使用ImageIO
,
extension URL{
var sizeOfImage: CGSize?{
guard let imageSource = CGImageSourceCreateWithURL(self as CFURL, nil)
, let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as? [AnyHashable: Any]
, let pixelWidth = imageProperties[kCGImagePropertyPixelWidth as String] as! CFNumber?
, let pixelHeight = imageProperties[kCGImagePropertyPixelHeight as String] as! CFNumber?
else {
return nil
}
var width: CGFloat = 0, height: CGFloat = 0
CFNumberGetValue(pixelWidth, .cgFloatType, &width)
CFNumberGetValue(pixelHeight, .cgFloatType, &height)
}
return CGSize(width: width, height: height)
}
}
imageProperties[kCGImagePropertyOrientation as String]
可能为空。
没有,我用png
图片文件进行了测试
kCGImagePropertyOrientation
此键的数值根据TIFF和Exif规范编码图像的预期显示方向。