1.直接調(diào)用SDWebImage里面的方法進(jìn)行加載,然后拿到圖片尺寸
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);
}];
特點(diǎn):在網(wǎng)絡(luò)圖片加載后,獲取圖片寬高時(shí)候比較適用,比較有局限性
2.通過(guò)UIImage的分類實(shí)現(xiàn)方法,一行代碼獲取圖片尺寸
/**
獲取網(wǎng)絡(luò)圖片高度
*/
+ (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);
//以下是對(duì)手機(jī)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
/***************** 此處解決返回圖片寬高相反問(wèn)題 *****************/
// 圖像旋轉(zhuǎn)的方向?qū)傩? NSInteger orientation = [(__bridge NSNumber *)CFDictionaryGetValue(imageProperties, kCGImagePropertyOrientation) integerValue];
CGFloat temp = 0;
switch (orientation) { // 如果圖像的方向不是正的,則寬高互換
case UIImageOrientationLeft: // 向左逆時(shí)針旋轉(zhuǎn)90度
case UIImageOrientationRight: // 向右順時(shí)針旋轉(zhuǎn)90度
case UIImageOrientationLeftMirrored: // 在水平翻轉(zhuǎn)之后向左逆時(shí)針旋轉(zhuǎn)90度
case UIImageOrientationRightMirrored: { // 在水平翻轉(zhuǎn)之后向右順時(shí)針旋轉(zhuǎn)90度
temp = width;
width = height;
height = temp;
}
break;
default:
break;
}
/***************** 此處解決返回圖片寬高相反問(wèn)題 *****************/
CFRelease(imageProperties);
}
CFRelease(imageSourceRef);
}
return CGSizeMake(width, height);
}
特點(diǎn):抽取出分類方法,使用在tableview的cell時(shí)候也可能會(huì)造成數(shù)據(jù)顯示出來(lái)的時(shí)間延長(zhǎng),圖片數(shù)量少的時(shí)候可以直接采用
3.通過(guò)圖片網(wǎng)址鏈接,然后再通過(guò)NSData直接UIImage即可獲得圖片的size
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"www.baidu.com"]];
UIImage *image = [UIImage imageWithData:data];
CGSize imageSize = image.size;
特點(diǎn):使用簡(jiǎn)便,使用在tableview的cell時(shí)候可能會(huì)造成數(shù)據(jù)顯示出來(lái)的時(shí)間延長(zhǎng),圖片數(shù)量少的時(shí)候可以直接采用
4.通過(guò) XHWebImageAutoSize來(lái)實(shí)現(xiàn)(推薦)
/**
* 參數(shù)1:圖片URL
* 參數(shù)2:imageView 寬度
* 參數(shù)3:預(yù)估高度,(此高度僅在圖片尚未加載出來(lái)前起作用,不影響真實(shí)高度)
*/
CGFloat imageHeight = [XHWebImageAutoSize imageHeightForURL:[NSURL URLWithString:@"www.baidu.com"] layoutWidth:width estimateHeight:256];
特點(diǎn):使用過(guò)程相對(duì)復(fù)雜一些,但是效果讓人很滿意,特別是tableview的cell需要大量圖片顯示時(shí)候,建議采用