美文网首页
iOS 根据URL获取 UIImage 图片宽高

iOS 根据URL获取 UIImage 图片宽高

作者: 阿拉灯神钉 | 来源:发表于2017-08-31 09:30 被阅读283次

    新建 UIImage分类

    #import <ImageIO/ImageIO.h>
    
    + (CGSize)yf_imageSizeWithURL:(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
                CFRelease(imageProperties);
            }
            
            CFRelease(imageSourceRef);
        }
        return CGSizeMake(width, height);
    }
    
    

    相关文章

      网友评论

          本文标题:iOS 根据URL获取 UIImage 图片宽高

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