刘少星


人的一切痛苦,本质上都是对自己无能的愤怒。加油!


welcome to mine blog !

IOS 获取网络图片大小

遇到很多次,大家都会去问,在获取服务器url的时候如何获取图片的大小;

之前的解决方案一直在回避这个问题,要么回答缓存下载到本地 然后去获取;不会影响到性能;还有就是让服务器把图片的大小也传过来

现在直面问题的 解决下这个问题

通过图片的类型分成几个部分来解析

JPG

+ (CGSize)downloadJPGImageSizeWithString:(NSString *)URLString
{
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URLString]];
    [request setValue:@"bytes=0-209" forHTTPHeaderField:@"Range"];
    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    if ([data length] <= 0x58) {
        return CGSizeZero;
    }

    if ([data length] < 210) {// 肯定只有一个DQT字段
        short w1 = 0, w2 = 0;
        [data getBytes:&w1 range:NSMakeRange(0x60, 0x1)];
        [data getBytes:&w2 range:NSMakeRange(0x61, 0x1)];
        short w = (w1 << 8) + w2;
        short h1 = 0, h2 = 0;
        [data getBytes:&h1 range:NSMakeRange(0x5e, 0x1)];
        [data getBytes:&h2 range:NSMakeRange(0x5f, 0x1)];
        short h = (h1 << 8) + h2;
        return CGSizeMake(w, h);
    } else {
        short word = 0x0;
        [data getBytes:&word range:NSMakeRange(0x15, 0x1)];
        if (word == 0xdb) {
            [data getBytes:&word range:NSMakeRange(0x5a, 0x1)];
            if (word == 0xdb) {// 两个DQT字段
                short w1 = 0, w2 = 0;
                [data getBytes:&w1 range:NSMakeRange(0xa5, 0x1)];
                [data getBytes:&w2 range:NSMakeRange(0xa6, 0x1)];
                short w = (w1 << 8) + w2;
                short h1 = 0, h2 = 0;
                [data getBytes:&h1 range:NSMakeRange(0xa3, 0x1)];
                [data getBytes:&h2 range:NSMakeRange(0xa4, 0x1)];
                short h = (h1 << 8) + h2;
                return CGSizeMake(w, h);
            } else {// 一个DQT字段
                short w1 = 0, w2 = 0;
                [data getBytes:&w1 range:NSMakeRange(0x60, 0x1)];
                [data getBytes:&w2 range:NSMakeRange(0x61, 0x1)];
                short w = (w1 << 8) + w2;
                short h1 = 0, h2 = 0;
                [data getBytes:&h1 range:NSMakeRange(0x5e, 0x1)];
                [data getBytes:&h2 range:NSMakeRange(0x5f, 0x1)];
                short h = (h1 << 8) + h2;
                return CGSizeMake(w, h);

            }
        } else {
            return CGSizeZero;
        }
    }
}

PNG

+ (CGSize)downloadPngImageSizeWithString:(NSString *)URLString
{
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URLString]];
    [request setValue:@"bytes=16-23" forHTTPHeaderField:@"Range"];
    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    if(data.length == 8)
    {
        int w1 = 0, w2 = 0, w3 = 0, w4 = 0;
        [data getBytes:&w1 range:NSMakeRange(0, 1)];
        [data getBytes:&w2 range:NSMakeRange(1, 1)];
        [data getBytes:&w3 range:NSMakeRange(2, 1)];
        [data getBytes:&w4 range:NSMakeRange(3, 1)];
        int w = (w1 << 24) + (w2 << 16) + (w3 << 8) + w4;
        int h1 = 0, h2 = 0, h3 = 0, h4 = 0;
        [data getBytes:&h1 range:NSMakeRange(4, 1)];
        [data getBytes:&h2 range:NSMakeRange(5, 1)];
        [data getBytes:&h3 range:NSMakeRange(6, 1)];
        [data getBytes:&h4 range:NSMakeRange(7, 1)];
        int h = (h1 << 24) + (h2 << 16) + (h3 << 8) + h4;
        return CGSizeMake(w, h);
    }
    return CGSizeZero;
}

GIF

+ (CGSize)downloadGIFImageSizeWithString:(NSString *)URLString
{
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URLString]];
    [request setValue:@"bytes=6-9" forHTTPHeaderField:@"Range"];
    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    if(data.length == 4)
    {
        short w1 = 0, w2 = 0;
        [data getBytes:&w1 range:NSMakeRange(1, 1)];
        [data getBytes:&w2 range:NSMakeRange(0, 1)];
        short w = (w1 << 8) + w2;

        short h1 = 0, h2 = 0;
        [data getBytes:&h1 range:NSMakeRange(3, 1)];
        [data getBytes:&h2 range:NSMakeRange(2, 1)];
        short h = (h1 << 8) + h2;

        return CGSizeMake(w, h);
    }
    return CGSizeZero;
}

把所有的格式统一起来

/**
 *  获取url的图片大小
 */
+ (CGSize)downloadImageSizeWithString:(NSString *)URLString;


+ (CGSize)downloadImageSizeWithString:(NSString *)URLString
{
    if ([URLString hasSuffix:@".jpg"]) {
        return [self downloadJPGImageSizeWithString:URLString];
    } else if ([URLString hasSuffix:@".png"]) {
        return [self downloadPngImageSizeWithString:URLString];
    } else if ([URLString hasSuffix:@".gif"]) {
        return [self downloadGIFImageSizeWithString:URLString];
    }
    return CGSizeZero;
}

最终封装成UIImage的分类来解决

UIImage+itdCategory.h

#import <UIKit/UIKit.h>

typedef void(^ImageSizeBlock)(CGSize size);

@interface UIImage (itdCategory)
/**
 *  获取网络url图片的具体大小
 */
+ (CGSize)itd_sizeOfImageWithUrlStr:(NSString *)imgUrlStr;

/**
 *  获取网络url图片的具体大小
 */
+ (void)itd_sizeOfImageWithUrlStr:(NSString *)imgUrlStr sizeGetDo:(ImageSizeBlock)doBlock;

@end

UIImage+itdCategory.m

#import "UIImage+itdCategory.h"

NSString *const kPngRangeValue = @"bytes=16-23";
NSString *const kJpgRangeValue = @"bytes=0-209";
NSString *const kGifRangeValue = @"bytes=6-9";

@implementation UIImage (itdCategory)

+ (CGSize)itd_sizeOfImageWithUrlStr:(NSString *)imgUrlStr{
    if ([imgUrlStr hasSuffix:@".png"]) {
        return [self size_downloadPNGImageWithUrlString:imgUrlStr];
    }else if ([imgUrlStr hasSuffix:@".gif"])
    {
        return [self size_downloadGIFImageWithUrlString:imgUrlStr];
    }else{
        return [self size_downloadJPGImageWithUrlString:imgUrlStr];
    }
    return CGSizeZero;
}


+ (void)itd_sizeOfImageWithUrlStr:(NSString *)imgUrlStr sizeGetDo:(ImageSizeBlock)doBlock
{
    NSString *extensionStr = [[imgUrlStr pathExtension] lowercaseString];
    CGSize imgSize = CGSizeZero;
    if ([extensionStr isEqualToString:@"png"]) {
        imgSize = [UIImage size_downloadPNGImageWithUrlString:imgUrlStr];
    }else if ([extensionStr isEqualToString:@"gif"])
    {
         imgSize = [UIImage size_downloadGIFImageWithUrlString:imgUrlStr];
    }else{
        imgSize = [UIImage size_downloadJPGImageWithUrlString:imgUrlStr];
    }

    if (doBlock) {
        doBlock(imgSize);
    }
    return ;
}

+ (CGSize)size_downloadJPGImageWithUrlString:(NSString *)urlString{
    NSString *URLString = urlString;
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URLString]];
    [request setValue:kJpgRangeValue forHTTPHeaderField:@"Range"];
    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    if (data == nil || [data length] <= 0x58) {
        return CGSizeZero;
    }

    if ([data length] < 210) {// 肯定只有一个DQT字段
        short w1 = 0, w2 = 0;
        [data getBytes:&w1 range:NSMakeRange(0x60, 0x1)];
        [data getBytes:&w2 range:NSMakeRange(0x61, 0x1)];
        short w = (w1 << 8) + w2;
        short h1 = 0, h2 = 0;
        [data getBytes:&h1 range:NSMakeRange(0x5e, 0x1)];
        [data getBytes:&h2 range:NSMakeRange(0x5f, 0x1)];
        short h = (h1 << 8) + h2;
        return CGSizeMake(w, h);
    } else {
        short word = 0x0;
        [data getBytes:&word range:NSMakeRange(0x15, 0x1)];
        if (word == 0xdb) {
            [data getBytes:&word range:NSMakeRange(0x5a, 0x1)];
            if (word == 0xdb) {// 两个DQT字段
                short w1 = 0, w2 = 0;
                [data getBytes:&w1 range:NSMakeRange(0xa5, 0x1)];
                [data getBytes:&w2 range:NSMakeRange(0xa6, 0x1)];
                short w = (w1 << 8) + w2;
                short h1 = 0, h2 = 0;
                [data getBytes:&h1 range:NSMakeRange(0xa3, 0x1)];
                [data getBytes:&h2 range:NSMakeRange(0xa4, 0x1)];
                short h = (h1 << 8) + h2;
                return CGSizeMake(w, h);
            } else {// 一个DQT字段
                short w1 = 0, w2 = 0;
                [data getBytes:&w1 range:NSMakeRange(0x60, 0x1)];
                [data getBytes:&w2 range:NSMakeRange(0x61, 0x1)];
                short w = (w1 << 8) + w2;
                short h1 = 0, h2 = 0;
                [data getBytes:&h1 range:NSMakeRange(0x5e, 0x1)];
                [data getBytes:&h2 range:NSMakeRange(0x5f, 0x1)];
                short h = (h1 << 8) + h2;
                return CGSizeMake(w, h);
            }
        } else {
            return CGSizeZero;
        }
    }
}

+ (CGSize)size_downloadPNGImageWithUrlString:(NSString *)urlString{
    NSString *URLString = urlString;
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URLString]];
    [request setValue:kPngRangeValue forHTTPHeaderField:@"Range"];
    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    if(data && data.length == 8)
    {
        int w1 = 0, w2 = 0, w3 = 0, w4 = 0;
        [data getBytes:&w1 range:NSMakeRange(0, 1)];
        [data getBytes:&w2 range:NSMakeRange(1, 1)];
        [data getBytes:&w3 range:NSMakeRange(2, 1)];
        [data getBytes:&w4 range:NSMakeRange(3, 1)];
        int w = (w1 << 24) + (w2 << 16) + (w3 << 8) + w4;
        int h1 = 0, h2 = 0, h3 = 0, h4 = 0;
        [data getBytes:&h1 range:NSMakeRange(4, 1)];
        [data getBytes:&h2 range:NSMakeRange(5, 1)];
        [data getBytes:&h3 range:NSMakeRange(6, 1)];
        [data getBytes:&h4 range:NSMakeRange(7, 1)];
        int h = (h1 << 24) + (h2 << 16) + (h3 << 8) + h4;
        return CGSizeMake(w, h);
    }
    return CGSizeZero;
}

+ (CGSize)size_downloadGIFImageWithUrlString:(NSString *)urlString{
    NSString *URLString = urlString;
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URLString]];
    [request setValue:kGifRangeValue forHTTPHeaderField:@"Range"];
    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    if(data && data.length == 4)
    {
        short w1 = 0, w2 = 0;
        [data getBytes:&w1 range:NSMakeRange(1, 1)];
        [data getBytes:&w2 range:NSMakeRange(0, 1)];
        short w = (w1 << 8) + w2;

        short h1 = 0, h2 = 0;
        [data getBytes:&h1 range:NSMakeRange(3, 1)];
        [data getBytes:&h2 range:NSMakeRange(2, 1)];
        short h = (h1 << 8) + h2;

        return CGSizeMake(w, h);
    }
    return CGSizeZero;
}

@end

开始你的IOS 获取网络图片大小之旅吧

最近的文章

了解GitHub工作流【译】

GitHub流是一个轻量级,基于分支的工作流,它使得小组和项目的部署变得标准化。这个向导解释了GitHub流是如何&为什么工作的。创建一个分支当你工作在一个项目中,你可能会在任何时间产生不同的想法和特性计划--这些都是准备完成的,或者其他不准备完成的。分支的存在可以帮助你管理工作流。当你在自己的项目中创建一个分支的时候,也就等于创建了一个尝试自己想法的环境。你在这个分支修改的东西不会影响到主分支,所以你可以尽情的测试和提交改变。这些改变直到你的合作伙伴审查过,确保安全才会被合并到主...…

Git继续阅读
更早的文章

如何简单使用极光推送

做过几次极光推送的iOS端;发现每次都有那么几个坑,希望能对你有帮助坑1导出的push的证书上没有小小的三角让你点击;让人感到诧异原因;因为你制作这个push证书的时候;你本机上没有私钥那么;很多人要问了 什么是私钥;我怎么能得到私有钥匙呢解决方法在制作push证书的时候会要求你导入一个钥匙串的东西那么问题来了;你是不是使用的之前的这个钥匙串呢?没有重新导出呢? 问题找到了因为每次 导出 钥匙串访问-从证书颁发机构请求证书 就会自动在机器上面生成私钥那么重新请求一次证书;你的电脑商就...…

继续阅读