先导入 ImageIO框架
#import <ImageIO/ImageIO.h>
- (CGSize)getImageSizeWithURL:(NSURL *)url
{
CGImageSourceRef image = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
CGFloat width = 0.0f, height = 0.0f;
if (image)
{
CFDictionaryRef imageAcc = CGImageSourceCopyPropertiesAtIndex(image, 0, NULL);
if (imageAcc != NULL)
{
CFNumberRef widthNumber = CFDictionaryGetValue(imageAcc, kCGImagePropertyPixelWidth);
if (widthNumber != NULL) {
CFNumberGetValue(widthNumber, kCFNumberFloatType, &width);
}
CFNumberRef heightNumber = CFDictionaryGetValue(imageAcc, kCGImagePropertyPixelHeight);
if (heightNumber != NULL) {
CFNumberGetValue(heightNumber, kCFNumberFloatType, &height);
}
CFRelease(imageAcc);
}
CFRelease(image);
NSLog(@"Image dimensions: %.0f x %.0f px", width, height);
}
return CGSizeMake(width, height);
}