123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- //
- // UIImage+getSize.m
- // LiDa
- //
- // Created by LF on 2017/7/31.
- // Copyright © 2017年 Simon. All rights reserved.
- //
- #import "UIImage+getSize.h"
- #import <ImageIO/ImageIO.h>
- @implementation UIImage (getSize)
- // 根据图片url获取图片尺寸
- +(CGSize )getImageSizeWithUrl:(id)imageURL {
- NSURL * url = nil;
- if ([imageURL isKindOfClass:[NSURL class]]) {
- url = imageURL;
- }
- if ([imageURL isKindOfClass:[NSString class]]) {
- url = [NSURL URLWithString:imageURL];
- }
- if (!imageURL) {
- return CGSizeZero;
- }
- CGImageSourceRef imageSourceRef = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
- CGFloat width = 0, height = 0;
- if (imageSourceRef) {
- CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSourceRef, 0, NULL);
- if (imageProperties != NULL) {
- CFNumberRef widthNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);
- if (widthNumberRef != NULL) {
- CFNumberGetValue(widthNumberRef, kCFNumberFloat64Type, &width);
- }
- CFNumberRef heightNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
- if (heightNumberRef != NULL) {
- CFNumberGetValue(heightNumberRef, kCFNumberFloat64Type, &height);
- }
- CFRelease(imageProperties);
- }
- CFRelease(imageSourceRef);
- }
- return CGSizeMake(width, height);
- // NSURL* URL = nil;
- // if([imageURL isKindOfClass:[NSURL class]]) {
- // URL = imageURL;
- // }
- // if([imageURL isKindOfClass:[NSString class]]) {
- // URL = [NSURL URLWithString:imageURL];
- // }
- // if(URL == nil) {
- // // url不正确返回CGSizeZero
- // return 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;
- }
- }
- }
- @end
|