到目前为止,我有这个方法:
+ (CGSize) imageDimensions: (NSString *)url {
NSURL *imageFileURL = [NSURL URLWithString: url];
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)imageFileURL, NULL);
if (imageSource == NULL) {
return CGSizeMake(0, 0);
}
CGFloat width = 0.0f, height = 0.0f;
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);
if (imageProperties != NULL) {
CFNumberRef widthNum = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);
if (widthNum != NULL) {
CFNumberGetValue(widthNum, kCFNumberFloatType, &width);
}
CFNumberRef heightNum = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
if (heightNum != NULL) {
CFNumberGetValue(heightNum, kCFNumberFloatType, &height);
}
CFRelease(imageProperties);
}
return CGSizeMake(width, height);
}
然而,我正在做一些测试,似乎这个方法的运行速度与我下载整个图像并检查 size 属性一样快。我原以为这种方法只需下载图像的元数据或标题并在那里检查分辨率即可。我错了吗?有没有一种快速的方法可以在不抓取图像的情况下尽快从目标 C 中的互联网图像中获取图像尺寸?