第一種方法:直接調用SDWebImage里面的方法進行加載然后拿到圖片尺寸
UIImageView *imageView = [[UIImageView alloc]init];
[imageView sd_setImageWithURL:[NSURL URLWithString:@"http://upload-images.jianshu.io/upload_images/2822163-70ac87aa2d2199d1.jpg"] placeholderImage:niloptions:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"寬:%f, 高:%f", image.size.width, image.size.height);
}];
第二種方法:一行代碼獲取圖片尺寸:
CGSize size = [UIImage getImageSizeWithURL:@"http://upload-images.jianshu.io/upload_images/2822163-70ac87aa2d2199d1.jpg"];
NSLog(@"寬:%f, 高:%f", size.width, size.height);
在使用之前還是要先引入系統的ImageIO.framework庫
UIImage+ImgSize.h 文件
#import <UIKit/UIKit.h>
@interface UIImage (ImgSize)
+ (CGSize)getImageSizeWithURL:(id)URL;
@end
UIImage+ImgSize.m 文件
#import "UIImage+ImgSize.h"
#import <ImageIO/ImageIO.h>
@implementation UIImage (ImgSize)
/**
* 根據圖片url獲取網絡圖片尺寸
*/
+ (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
/***************** 此處解決返回圖片寬高相反問題 *****************/
// 圖像旋轉的方向屬性
NSInteger orientation = [(__bridge NSNumber *)CFDictionaryGetValue(imageProperties, kCGImagePropertyOrientation) integerValue];
CGFloat temp = 0;
switch (orientation) { // 如果圖像的方向不是正的,則寬高互換
case UIImageOrientationLeft: // 向左逆時針旋轉90度
case UIImageOrientationRight: // 向右順時針旋轉90度
case UIImageOrientationLeftMirrored: // 在水平翻轉之后向左逆時針旋轉90度
case UIImageOrientationRightMirrored: { // 在水平翻轉之后向右順時針旋轉90度
temp = width;
width = height;
height = temp;
}
break;
default:
break;
}
/***************** 此處解決返回圖片寬高相反問題 *****************/
CFRelease(imageProperties);
}
CFRelease(imageSourceRef);
}
return CGSizeMake(width, height);
}
@end
有網友提到這個方法應用到cell里面會卡頓,這里給出我的解決方法:
在拿到需要請求的url數組的時候,就將每個鏈接的尺寸順便就給獲取出來,然后本地化存儲該圖片的尺寸,然后再到cell里面根據鏈接直接在本地取到圖片的尺寸,那樣在cell里面瀏覽的時候就不會有卡頓了。
//獲取圖片尺寸時先檢查是否有緩存(有就不用再獲取了)
if (![[NSUserDefaults standardUserDefaults] objectForKey:url]) {
//這里拿到每個圖片的尺寸,然后計算出每個cell的高度
CGSize imageSize = [UIImage getImageSizeWithURL:url];
CGFloat imgH = 0;
if (imageSize.height > 0) {
//這里就把圖片根據 固定的需求寬度 計算 出圖片的自適應高度
imgH = imageSize.height * (SCREEN_WIDTH - 2 * _spaceX) / imageSize.width;
}
//將最終的自適應的高度 本地化處理
[[NSUserDefaults standardUserDefaults] setObject:@(imgH) forKey:url];
}
本文相關資料鏈接(關于方向問題即寬高相反)
1、CGImageSource對圖像數據讀取任務的抽象:
http://www.tanhao.me/pieces/1019.html
2、iOS開發中圖片方向的獲取與更改:
http://www.cnblogs.com/gaoxiaoniu/p/5329834.html
以下是圖片素材:
png圖片.png
jpg圖片.jpg
gif圖片.gif
Github 地址:https://github.com/90candy/GetImageSizeWithURL