獲取網路圖片的尺寸大小
在iOS開發過程中經常需要通過網路請求載入圖片,有時,需要在建立UIImageView或UIButton來顯示圖片之前需要提前知道圖片的尺寸,根據圖片尺寸建立對應大小的控制元件。但是對於網路圖片來說,要想通過最優的方法獲得尺寸就略微有點困難,大體思路有這麼幾種:
1.通過伺服器處理。即在下行圖片路徑時拼接該圖片的寬高。這種方法最簡單,避免了不必要的網路請求,只需要從URL中擷取即可。
2.網路請求。先通過檔案頭獲取圖片大小,如果獲取失敗,則下載完整的圖片data,然後計算大小;如果有使用SDWebImage,則首先檢查是否快取過該圖片,下面針對格式為png、fig、jpg檔案獲取其尺寸大小,直接上程式碼:
首先巨集定義一個單例實現
// @interface
#define singleton_interface(className) \
+ (className *)shared##className;
// @implementation
#define singleton_implementation(className) \
static className *_instance; \
+ (id)allocWithZone:(NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
} \
+ (className *)shared##className \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[self alloc] init]; \
}); \
return _instance; \
}
********************************************************************然後標頭檔案如下
#import <Foundation/Foundation.h>
#import "Singleton.h"
@interface BLImageSize : NSObject
singleton_interface(BLImageSize)
/**
獲取網路圖片的Size, 先通過檔案頭來獲取圖片大小
如果失敗 會下載完整的圖片Data 來計算大小所以最好別放在主執行緒
如果你有使用SDWebImage就會先看下 SDWebImage有快取過改圖片沒有
支援檔案頭大小的格式 png、gif、jpg
*/
+(CGSize)downloadImageSizeWithURL:(id)imageURL;
@end
*************************************************************************
最後是.m檔案實現
#import "BLImageSize.h"
#import "SDImageCache.h"
@implementation BLImageSize
singleton_implementation(BLImageSize)
+(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;
NSString* absoluteString = URL.absoluteString;
#ifdef dispatch_main_sync_safe
if([[SDImageCachesharedImageCache] diskImageExistsWithKey:absoluteString])
{
UIImage* image = [[SDImageCachesharedImageCache] 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 = [[NSMutableURLRequestalloc] initWithURL:URL];
NSString* pathExtendsion = [URL.pathExtension lowercaseString];
CGSize size = CGSizeZero;
if([pathExtendsion isEqualToString:@"png"]){
size = [selfdownloadPNGImageSizeWithRequest:request];
}
else if([pathExtendsion isEqual:@"gif"])
{
size = [selfdownloadGIFImageSizeWithRequest:request];
}
else{
size = [selfdownloadJPGImageSizeWithRequest:request];
}
if(CGSizeEqualToSize(CGSizeZero, size))
{
NSData* data = [NSURLConnectionsendSynchronousRequest:[NSURLRequestrequestWithURL:URL] returningResponse:nilerror:nil];
UIImage* image = [UIImage imageWithData:data];
if(image)
{
#ifdef dispatch_main_sync_safe
[[SDImageCachesharedImageCache] storeImage:image recalculateFromImage:YESimageData:data forKey:URL.absoluteStringtoDisk:YES];
#endif
size = image.size;
}
}
return size;
}
+(CGSize)downloadPNGImageSizeWithRequest:(NSMutableURLRequest*)request
{
[request setValue:@"bytes=16-23"forHTTPHeaderField:@"Range"];
NSData* data = [NSURLConnectionsendSynchronousRequest:request returningResponse:nilerror: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);
}
returnCGSizeZero;
}
+(CGSize)downloadGIFImageSizeWithRequest:(NSMutableURLRequest*)request
{
[request setValue:@"bytes=6-9"forHTTPHeaderField:@"Range"];
NSData* data = [NSURLConnectionsendSynchronousRequest:request returningResponse:nilerror: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);
}
returnCGSizeZero;
}
+(CGSize)downloadJPGImageSizeWithRequest:(NSMutableURLRequest*)request
{
[request setValue:@"bytes=0-209"forHTTPHeaderField:@"Range"];
NSData* data = [NSURLConnectionsendSynchronousRequest:request returningResponse:nilerror: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