导入
#import@interface UIImage (ImgSize)/** * 根据图片url获取网络图片尺寸 */+ (CGSize)getImageSizeWithURL:(id)URL;@end
#import "UIImage+ImgSize.h"#import@implementation UIImage (ImgSize)+ (CGSize)getImageSizeWithURL:(id)URL{ NSURL * url = nil; if ([URL isKindOfClass:[NSURL class]]) { url = URL; } if ([URL isKindOfClass:[NSString class]]) { url = [NSURL URLWithString:URL]; } if (!URL) { return CGSizeZero; } CGImageSourceRef imageSourceRef = CGImageSourceCreateWithURL((CFURLRef)url, NULL); CGFloat width = 0, height = 0; if (imageSourceRef) { CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSourceRef, 0, NULL); // 以下是对手机32位、64位的处理(由网友评论区拿到的:小怪兽饲养猿) if (imageProperties != NULL) { CFNumberRef widthNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);#if defined(__LP64__) && __LP64__ if (widthNumberRef != NULL) { CFNumberGetValue(widthNumberRef, kCFNumberFloat64Type, &width); } CFNumberRef heightNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight); if (heightNumberRef != NULL) { CFNumberGetValue(heightNumberRef, kCFNumberFloat64Type, &height); }#else if (widthNumberRef != NULL) { CFNumberGetValue(widthNumberRef, kCFNumberFloat32Type, &width); } CFNumberRef heightNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight); if (heightNumberRef != NULL) { CFNumberGetValue(heightNumberRef, kCFNumberFloat32Type, &height); }#endif CFRelease(imageProperties); } CFRelease(imageSourceRef); } return CGSizeMake(width, height);}@end