iOS獲取網(wǎng)絡(luò)圖片的寬高

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í)候,建議采用

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容