到目前为止,我有这个方法:
+ (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);
}
然而,我做了一些测试,发现这种方法的速度只有当我下载整个图像并检查大小属性时才能达到同样的速度。我原以为这种方法是通过仅下载图像的元数据或标头并在那里检查分辨率来工作的。我错了吗?在objective C中,有没有一种快速获取互联网图像尺寸的方法,而不需要抓取图像?