在开发过程中,我们经常需要通过网络请求加载图片。有时,我们需要在创建UIImageView时就知道请求图片的尺寸,根据图片尺寸来设置控件的Frame。如微信朋友圈,新浪微博,在图片未加载出来之前,控件的Frame就已经设置好。(图片宽高不一致100✘200或者 400 ✘ 150)。
- 通过服务器返回图片尺寸设置
这种方法是体验最好的方法。在后台返回url的同时,返回图片宽高,再设置控件frame; - 通过获取image文件头方法
//类方法 传入图片url 返回图片size
+(CGSize)downloadImageSizeWithURL:(id)imageURL{
NSURL* URL = nil;
//判断url类型
if([imageURL isKindOfClass:[NSURL class]]){
URL = imageURL;
}
if([imageURL isKindOfClass:[NSString class]]){
URL = [NSURL URLWithString:imageURL];
}
if(URL == nil)
return CGSizeZero;
NSString* absoluteString = URL.absoluteString;
//通过SDWebimage 查看图片是否已有缓存,如果有直接获取,如果没有,请求文件头
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;
}
}
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];
NSString* pathExtendsion = [URL.pathExtension lowercaseString];
CGSize size = CGSizeZero;
//区分加载图片的类型
if([pathExtendsion isEqualToString:@"png"]){
size = [self downloadPNGImageSizeWithRequest:request];
}else if([pathExtendsion isEqual:@"gif"]){
size = [self downloadGIFImageSizeWithRequest:request];
}else{
size = [self downloadJPGImageSizeWithRequest:request];
}
if(CGSizeEqualToSize(CGSizeZero, size)){
NSData* data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:URL] returningResponse:nil error:nil];
UIImage* image = [UIImage imageWithData:data];
if(image){
[[SDImageCache sharedImageCache] storeImage:image recalculateFromImage:YES imageData:data forKey:URL.absoluteString toDisk:YES];
size = image.size;
}
}
return size;
}
//PNG格式的图片
+(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 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)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 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)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 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;
}
}
}
该方法通过请求少量字节的网络请求,获取到图片的文件头中的尺寸信息,效果还是比较理想的。
但是,如果在网络环境较差的情况下,这种方法又会有一个致命的弱点--同步请求
。若自定义cell中用此方法获取图片尺寸, 较差的网络环境,TableView的不断滚动,可以明显感受到主线程阻塞带来的卡顿。
总结
如果在自定义cell中想预先获取加载图片的尺寸,最理想的方法还是与后台协商返回吧!
当然,还可以通过SDWebImage获取
[imageView sd_setImageWithURL: imageUrl completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//图片的宽
CGFloat width = image.size.width;
//图片的高
CGFloat height = image.size.height;
}];
异步加载完毕后,重新布局cell,设置高度,reload当前cell。