美文网首页iOS学习专题iOS技术收藏
iOS获取网络图片的宽高

iOS获取网络图片的宽高

作者: 琦玉老师很强 | 来源:发表于2020-07-20 12:34 被阅读0次

    1.直接调用SDWebImage里面的方法进行加载,然后拿到图片尺寸

    UIImageView *imageView = [[UIImageView alloc]init];
    [imageView sd_setImageWithURL:[NSURL URLWithString:@"https://img.haomeiwen.com/i2822163/70ac87aa2d2199d1.jpg"] placeholderImage:niloptions:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        NSLog(@"宽:%f, 高:%f", image.size.width, image.size.height);
    }];
    

    特点:在网络图片加载后,获取图片宽高时候比较适用,比较有局限性

    2.通过UIImage的分类实现方法,一行代码获取图片尺寸

    /**
     获取网络图片高度
     */
    + (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);  
    }
    

    特点:抽取出分类方法,使用在tableview的cell时候也可能会造成数据显示出来的时间延长,图片数量少的时候可以直接采用

    3.通过图片网址链接,然后再通过NSData直接UIImage即可获得图片的size

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"www.baidu.com"]];
    UIImage *image = [UIImage imageWithData:data];
    CGSize imageSize = image.size;
    

    特点:使用简便,使用在tableview的cell时候可能会造成数据显示出来的时间延长,图片数量少的时候可以直接采用

    4.通过 XHWebImageAutoSize来实现(推荐)

    /**
         *  参数1:图片URL
         *  参数2:imageView 宽度
         *  参数3:预估高度,(此高度仅在图片尚未加载出来前起作用,不影响真实高度)
         */
     CGFloat imageHeight = [XHWebImageAutoSize imageHeightForURL:[NSURL URLWithString:@"www.baidu.com"] layoutWidth:width estimateHeight:256];
    

    特点:使用过程相对复杂一些,但是效果让人很满意,特别是tableview的cell需要大量图片显示时候,建议采用

    相关文章

      网友评论

        本文标题:iOS获取网络图片的宽高

        本文链接:https://www.haomeiwen.com/subject/gppekktx.html