正如你所知,iPhone的指導方針不鼓勵加載超過1024x1024的uiimages。訪問UIImage屬性而不在內存中加載圖像
我將不得不加載的圖像的大小各不相同,我想檢查我即將加載的圖像的大小;然而使用uiimage的.size屬性需要圖像被覆蓋......這正是我想要避免的。
我的推理有什麼問題嗎?或者有解決方案嗎?
謝謝大家
正如你所知,iPhone的指導方針不鼓勵加載超過1024x1024的uiimages。訪問UIImage屬性而不在內存中加載圖像
我將不得不加載的圖像的大小各不相同,我想檢查我即將加載的圖像的大小;然而使用uiimage的.size屬性需要圖像被覆蓋......這正是我想要避免的。
我的推理有什麼問題嗎?或者有解決方案嗎?
謝謝大家
由於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和拉登,上市9.5,第228頁)
爲什麼使用'CGImageSourceCopyPropertiesAtIndex'而不是'CGImageSourceCopyProperties'? – jcm 2012-02-13 04:28:04
因爲變量'width'和'height'被聲明爲'CGFloat',所以在調用CFNumberGetValue()時通過'kCFNumberCGFloatType'而不是'kCFNumberFloatType'是非常重要的。上面的代碼可以在32位系統上運行,但是這些值在64位系統上會包含垃圾。 – fjoachim 2013-12-05 08:30:55
@fjoachim:謝謝,修正。 – 2013-12-05 11:42:40
斯威夫特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)
}
這是一個很好的問題。 Android提供了一種手段來做到這一點,但我不知道iOS的解決方案。編輯:這已被問過。 http://stackoverflow.com/questions/1551300/get-size-of-image-without-loading-in-to-memory – Justin 2010-11-12 22:37:09
我之前搜索過,但我找不到它!非常感謝! – koda 2010-11-13 02:05:16