IOS 获取网络图片大小实例详解
IOS获取网络图片大小实例详解
在iOS开发过程中经常需要通过网络请求加载图片,有时,需要在创建UIImageView或UIButton来显示图片之前需要提前知道图片的尺寸,根据图片尺寸创建对应大小的控件。但是对于网络图片来说,要想通过最优的方法获得尺寸就略微有点困难,大体思路就是下面这种:
如果有使用SDWebImage,则首先检查是否缓存过该图片,如果没有,先通过文件头获取图片大小(针对格式为png、gif、jpg文件获取其尺寸大小),如果获取失败,则下载完整的图片data,然后计算大小,如果有使用SDWebImage,则使用SDWebImage缓存该图片。
实例代码:
+(CGSize)downloadImageSizeWithURL:(id)imageURL
{
NSURL*URL=nil;
if([imageURLisKindOfClass:[NSURLclass]]){
URL=imageURL;
}
if([imageURLisKindOfClass:[NSStringclass]]){
URL=[NSURLURLWithString:imageURL];
}
if(URL==nil)
returnCGSizeZero;
#ifdefdispatch_main_sync_safe
if([[SDImageCachesharedImageCache]diskImageExistsWithKey:absoluteString])
{
UIImage*image=[[SDImageCachesharedImageCache]imageFromMemoryCacheForKey:absoluteString];
if(!image)
{
NSData*data=[[SDImageCachesharedImageCache]performSelector:@selector(diskImageDataBySearchingAllPathsForKey:)withObject:URL.absoluteString];
image=[UIImageimageWithData:data];
}
if(image)
{
returnimage.size;
}
}
#endif
NSMutableURLRequest*request=[[NSMutableURLRequestalloc]initWithURL:URLcachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:1];
NSString*pathExtendsion=[URL.pathExtensionlowercaseString];
CGSizesize=CGSizeZero;
if([pathExtendsionrangeOfString:@"png"].location!=NSNotFound){
size=[selfdownloadPNGImageSizeWithRequest:request];
}
elseif([pathExtendsionrangeOfString:@"gif"].location!=NSNotFound)
{
size=[selfdownloadGIFImageSizeWithRequest:request];
}
else{
size=[selfdownloadJPGImageSizeWithRequest:request];
}
if(CGSizeEqualToSize(CGSizeZero,size))
{
NSData*data=[NSDatadataWithContentsOfURL:URL];
UIImage*image=[UIImageimageWithData:data];
if(image)
{
//如果未使用SDWebImage,则忽略;缓存该图片
#ifdefdispatch_main_sync_safe
[[SDImageCachesharedImageCache]storeImage:imagerecalculateFromImage:YESimageData:dataforKey:URL.absoluteStringtoDisk:YES];
#endif
size=image.size;
}
}
//过滤掉不符合大小的图片,大图太大浪费流量,用户体验不好
if(size.height>2048||size.height<=0||size.width>2048||size.width<=0){
returnCGSizeZero;
}
else
{
returnsize;
}
}
+(CGSize)downloadPNGImageSizeWithRequest:(NSMutableURLRequest*)request
{
[requestsetValue:@"bytes=16-23"forHTTPHeaderField:@"Range"];
NSData*data=[NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:nil];
if(data.length==8)
{
intw1=0,w2=0,w3=0,w4=0;
[datagetBytes:&w1range:NSMakeRange(0,1)];
[datagetBytes:&w2range:NSMakeRange(1,1)];
[datagetBytes:&w3range:NSMakeRange(2,1)];
[datagetBytes:&w4range:NSMakeRange(3,1)];
intw=(w1<<24)+(w2<<16)+(w3<<8)+w4;
inth1=0,h2=0,h3=0,h4=0;
[datagetBytes:&h1range:NSMakeRange(4,1)];
[datagetBytes:&h2range:NSMakeRange(5,1)];
[datagetBytes:&h3range:NSMakeRange(6,1)];
[datagetBytes:&h4range:NSMakeRange(7,1)];
inth=(h1<<24)+(h2<<16)+(h3<<8)+h4;
returnCGSizeMake(w,h);
}
returnCGSizeZero;
}
+(CGSize)downloadGIFImageSizeWithRequest:(NSMutableURLRequest*)request
{
[requestsetValue:@"bytes=6-9"forHTTPHeaderField:@"Range"];
NSData*data=[NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:nil];
if(data.length==4)
{
shortw1=0,w2=0;
[datagetBytes:&w1range:NSMakeRange(0,1)];
[datagetBytes:&w2range:NSMakeRange(1,1)];
shortw=w1+(w2<<8);
shorth1=0,h2=0;
[datagetBytes:&h1range:NSMakeRange(2,1)];
[datagetBytes:&h2range:NSMakeRange(3,1)];
shorth=h1+(h2<<8);
returnCGSizeMake(w,h);
}
returnCGSizeZero;
}
+(CGSize)downloadJPGImageSizeWithRequest:(NSMutableURLRequest*)request
{
[requestsetValue:@"bytes=0-209"forHTTPHeaderField:@"Range"];
NSData*data=[NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:nil];
if([datalength]<=0x58){
returnCGSizeZero;
}
if([datalength]<210){//肯定只有一个DQT字段
shortw1=0,w2=0;
[datagetBytes:&w1range:NSMakeRange(0x60,0x1)];
[datagetBytes:&w2range:NSMakeRange(0x61,0x1)];
shortw=(w1<<8)+w2;
shorth1=0,h2=0;
[datagetBytes:&h1range:NSMakeRange(0x5e,0x1)];
[datagetBytes:&h2range:NSMakeRange(0x5f,0x1)];
shorth=(h1<<8)+h2;
returnCGSizeMake(w,h);
}else{
shortword=0x0;
[datagetBytes:&wordrange:NSMakeRange(0x15,0x1)];
if(word==0xdb){
[datagetBytes:&wordrange:NSMakeRange(0x5a,0x1)];
if(word==0xdb){//两个DQT字段
shortw1=0,w2=0;
[datagetBytes:&w1range:NSMakeRange(0xa5,0x1)];
[datagetBytes:&w2range:NSMakeRange(0xa6,0x1)];
shortw=(w1<<8)+w2;
shorth1=0,h2=0;
[datagetBytes:&h1range:NSMakeRange(0xa3,0x1)];
[datagetBytes:&h2range:NSMakeRange(0xa4,0x1)];
shorth=(h1<<8)+h2;
returnCGSizeMake(w,h);
}else{//一个DQT字段
shortw1=0,w2=0;
[datagetBytes:&w1range:NSMakeRange(0x60,0x1)];
[datagetBytes:&w2range:NSMakeRange(0x61,0x1)];
shortw=(w1<<8)+w2;
shorth1=0,h2=0;
[datagetBytes:&h1range:NSMakeRange(0x5e,0x1)];
[datagetBytes:&h2range:NSMakeRange(0x5f,0x1)];
shorth=(h1<<8)+h2;
returnCGSizeMake(w,h);
}
}else{
returnCGSizeZero;
}
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!