IOS获取网络图片大小实例详解-创新互联
IOS 获取网络图片大小实例详解
成都网站设计、做网站介绍好的网站是理念、设计和技术的结合。创新互联拥有的网站设计理念、多方位的设计风格、经验丰富的设计团队。提供PC端+手机端网站建设,用营销思维进行网站设计、采用先进技术开源代码、注重用户体验与SEO基础,将技术与创意整合到网站之中,以契合客户的方式做到创意性的视觉化效果。在iOS开发过程中经常需要通过网络请求加载图片,有时,需要在创建UIImageView或UIButton来显示图片之前需要提前知道图片的尺寸,根据图片尺寸创建对应大小的控件。但是对于网络图片来说,要想通过最优的方法获得尺寸就略微有点困难,大体思路就是下面这种:
如果有使用SDWebImage,则首先检查是否缓存过该图片,如果没有,先通过文件头获取图片大小(针对格式为png、gif、jpg文件获取其尺寸大小),如果获取失败,则下载完整的图片data,然后计算大小,如果有使用SDWebImage,则使用SDWebImage缓存该图片。
实例代码:
+(CGSize)downloadImageSizeWithURL:(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; #ifdef dispatch_main_sync_safe if([[SDImageCache sharedImageCache] diskImageExistsWithKey:absoluteString]) { UIImage* image = [[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:absoluteString]; if(!image) { NSData* data = [[SDImageCache sharedImageCache] performSelector:@selector(diskImageDataBySearchingAllPathsForKey:) withObject:URL.absoluteString]; image = [UIImage imageWithData:data]; } if(image) { return image.size; } } #endif NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1]; NSString* pathExtendsion = [URL.pathExtension lowercaseString]; CGSize size = CGSizeZero; if ([pathExtendsion rangeOfString:@"png"].location != NSNotFound) { size = [self downloadPNGImageSizeWithRequest:request]; } else if([pathExtendsion rangeOfString:@"gif"].location != NSNotFound) { size = [self downloadGIFImageSizeWithRequest:request]; } else{ size = [self downloadJPGImageSizeWithRequest:request]; } if(CGSizeEqualToSize(CGSizeZero, size)) { NSData* data = [NSData dataWithContentsOfURL:URL]; UIImage* image = [UIImage imageWithData:data]; if(image) { //如果未使用SDWebImage,则忽略;缓存该图片 #ifdef dispatch_main_sync_safe [[SDImageCache sharedImageCache] storeImage:image recalculateFromImage:YES imageData:data forKey:URL.absoluteString toDisk:YES]; #endif size = image.size; } } //过滤掉不符合大小的图片,大图太大浪费流量,用户体验不好 if (size.height > 2048 || size.height <= 0 || size.width > 2048 || size.width <= 0 ) { return CGSizeZero; } else { return size; } } +(CGSize)downloadPNGImageSizeWithRequest:(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 h2 = 0, h3 = 0, h4 = 0, h5 = 0; [data getBytes:&h2 range:NSMakeRange(4, 1)]; [data getBytes:&h3 range:NSMakeRange(5, 1)]; [data getBytes:&h4 range:NSMakeRange(6, 1)]; [data getBytes:&h5 range:NSMakeRange(7, 1)]; int h = (h2 << 24) + (h3 << 16) + (h4 << 8) + h5; return CGSizeMake(w, h); } return CGSizeZero; } +(CGSize)downloadGIFImageSizeWithRequest:(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 h2 = 0, h3 = 0; [data getBytes:&h2 range:NSMakeRange(2, 1)]; [data getBytes:&h3 range:NSMakeRange(3, 1)]; short h = h2 + (h3 << 8); return CGSizeMake(w, h); } return CGSizeZero; } +(CGSize)downloadJPGImageSizeWithRequest:(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 h2 = 0, h3 = 0; [data getBytes:&h2 range:NSMakeRange(0x5e, 0x1)]; [data getBytes:&h3 range:NSMakeRange(0x5f, 0x1)]; short h = (h2 << 8) + h3; 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 h2 = 0, h3 = 0; [data getBytes:&h2 range:NSMakeRange(0xa3, 0x1)]; [data getBytes:&h3 range:NSMakeRange(0xa4, 0x1)]; short h = (h2 << 8) + h3; 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 h2 = 0, h3 = 0; [data getBytes:&h2 range:NSMakeRange(0x5e, 0x1)]; [data getBytes:&h3 range:NSMakeRange(0x5f, 0x1)]; short h = (h2 << 8) + h3; return CGSizeMake(w, h); } } else { return CGSizeZero; } } }
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章名称:IOS获取网络图片大小实例详解-创新互联
文章链接:http://myzitong.com/article/dpcohh.html