ios根据图片url获取图片的尺寸

这篇博客介绍了如何在Objective-C中通过UIImage的分类方法,根据图片URL获取网络图片的尺寸。通过CGImageSourceRef和CFDictionaryRef处理图像属性,考虑了不同设备的位宽和图像方向,确保正确返回图片的宽高。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在UIImage的分类中添加该方法

 

/**

 *  根据图片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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值