0%

获取图片的宽高

闲话不多说,上码。

1、获取本地图片宽高

1
2
3
4
5
6
7
8
/**
* 本地图片宽高的获取
*/
UIImage *img = [UIImage imageNamed:@"test.jpg"];
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
imgView.frame = CGRectMake(0, 0, img.size.width, img.size.height);
[self.view addSubview:imgView];
NSLog(@"===%f===%f",img.size.width,img.size.width);

2、加载互联网图片

  • SDWebImage下载图片,图片被缓存到本地获取本地图片的宽高。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    /**
    * sdwebimage获取图片宽高
    */
    UIImageView * sdImgView = [UIImageView new];
    [self.view addSubview:sdImgView];
    NSURL * url = [NSURL URLWithString:@"http://appceshi.ywsoftware.com/OneCarAPI/rechargeCard/logo/C9100.jpg"];
    [sdImgView sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    NSLog(@"%f==%f",image.size.height,image.size.width);
    sdImgView.frame = CGRectMake(0, CGRectGetMaxY(imgView.frame)+20, image.size.width, image.size.height);
    }];
  • 网络框架加载:先通过文件头获取图片大小(没什么卵用),如果获取失败,则下载完整的图片data,然后计算大小
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    -(CGSize)getImageSizeWithURL:(id)imageURL
    {
    NSURL* URL = nil;
    if([imageURL isKindOfClass:[NSURL class]]){
    URL = imageURL;
    }
    if([imageURL isKindOfClass:[NSString class]]){
    URL = [NSURL URLWithString:imageURL];
    }
    if(URL == nil)
    return CGSizeZero; // url不正确返回CGSizeZero

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];
    NSString* pathExtendsion = [URL.pathExtension lowercaseString];

    CGSize size = CGSizeZero;
    if([pathExtendsion isEqualToString:@"png"]){
    size = [self getPNGImageSizeWithRequest:request];
    }
    else if([pathExtendsion isEqual:@"gif"])
    {
    size = [self getGIFImageSizeWithRequest:request];
    }
    else{
    size = [self getJPGImageSizeWithRequest:request];
    }
    if(CGSizeEqualToSize(CGSizeZero, size)) // 如果获取文件头信息失败,发送异步请求请求原图
    {

    NSData* data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:URL] returningResponse:nil error:nil];

    UIImage* image = [UIImage imageWithData:data];
    if(image)
    {
    size = image.size;
    }
    }
    return size;
    }
    // 获取PNG图片的大小
    -(CGSize)getPNGImageSizeWithRequest:(NSMutableURLRequest*)request
    {
    [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)getGIFImageSizeWithRequest:(NSMutableURLRequest*)request
    {
    [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(0, 1)];
    [data getBytes:&w2 range:NSMakeRange(1, 1)];
    short w = w1 + (w2 << 8);
    short h1 = 0, h2 = 0;
    [data getBytes:&h1 range:NSMakeRange(2, 1)];
    [data getBytes:&h2 range:NSMakeRange(3, 1)];
    short h = h1 + (h2 << 8);
    return CGSizeMake(w, h);
    }
    return CGSizeZero;
    }
    // 获取jpg图片的大小
    -(CGSize)getJPGImageSizeWithRequest:(NSMutableURLRequest*)request
    {
    [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;
    }
    }
    }