通過服務器獲取
后臺開發人員在返回圖片地址的同時,返回圖片尺寸比例數據。
利用ImageIO創建CGImageSourceRef對象
此方法會下載圖片,且同步執行。
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)[NSURL URLWithString:fileurl], NULL);
CGFloat width = 0, height = 0;
if (imageSource)
{
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);
if (imageProperties != NULL)
{
CFNumberRef widthNum = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);
if (widthNum != NULL) {
CFNumberGetValue(widthNum, kCFNumberFloat64Type, &width);
}
CFNumberRef heightNum = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
if (heightNum != NULL) {
CFNumberGetValue(heightNum, kCFNumberFloat64Type, &height);
}
CFRelease(imageProperties);
}
CFRelease(imageSource);
}