iOS 獲取網路圖片的尺寸
// 1、直接獲取
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:serverUrl]]; UIImage *showimage = [UIImage imageWithData:data]; DDLOG(@"w = %f,h = %f",showimage.size.width,showimage.size.height); CGFloat scale = showimage.size.height/showimage.size.width; self.scrollView.contentSize = CGSizeMake(MSWIDTH, MSWIDTH * scale);
//2、需要用到第三方庫SDWebImage
UIImageView *v1 = [[UIImageViewalloc]init];
[self.viewaddSubview:v1];
[v1 sd_setImageWithURL:[NSURLURLWithString:serverUrl]placeholderImage:[UIImageimageNamed:@"bg2"]completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType,NSURL *imageURL) {
CGSize
CGFloat w = size.width;
CGFloat H = size.height;
CGFloat scale = H/w;
DDLOG(@"++++++++++++++++++%.2f------%.2f",size.width,size.height);
self.scrollView.contentSize =CGSizeMake(MSWIDTH,MSWIDTH * scale);
picImageView.frame =CGRectMake(0,0,MSWIDTH,self.scrollView.contentSize
}];
//3、同步載入
CGSize size = [DetailViewControllergetImageSizeWithURL:[NSURLURLWithString:serverUrl]];
//獲取圖片的大小
+(CGSize)getImageSizeWithURL:(id)imageURL
{
NSURL* URL =nil;
if([imageURLisKindOfClass:[NSURLclass]]){
URL = imageURL;
}
if([imageURLisKindOfClass:[NSStringclass]]){
URL = [NSURLURLWithString:imageURL];
}
if(URL ==nil)
returnCGSizeZero; // url不正確返回CGSizeZero
NSMutableURLRequest *request = [[NSMutableURLRequestalloc]initWithURL:URL];
NSString* pathExtendsion = [URL.pathExtensionlowercaseString];
CGSize size =CGSizeZero;
if([pathExtendsionisEqualToString:@"png"]){
size = [selfgetPNGImageSizeWithRequest:request];
}else{
size = [selfgetJPGImageSizeWithRequest:request];
}
if(CGSizeEqualToSize(CGSizeZero, size)) //如果獲取檔案頭資訊失敗,傳送非同步請求請求原圖
{
NSData* data = [NSURLConnectionsendSynchronousRequest:[NSURLRequestrequestWithURL:URL]returningResponse:nilerror:nil];
UIImage* image = [UIImageimageWithData:data];
if(image)
{
size = image.size;
}
}
return size;
}
// 獲取PNG圖片的大小
+(CGSize)getPNGImageSizeWithRequest:(NSMutableURLRequest*)request
{
[request setValue:@"bytes=16-23"forHTTPHeaderField:@"Range"];
NSData* data = [NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:nil];
if(data.length ==8)
{
int w1 =0, w2 =0, w3 =0, w4 =0;
[data getBytes:&w1range:NSMakeRange(0,1)];
[data getBytes:&w2range:NSMakeRange(1,1)];
[data getBytes:&w3range:NSMakeRange(2,1)];
[data getBytes:&w4range:NSMakeRange(3,1)];
int w = (w1 <<24) + (w2 <<16) + (w3 <<8) + w4;
int h1 =0, h2 =0, h3 =0, h4 =0;
[data getBytes:&h1range:NSMakeRange(4,1)];
[data getBytes:&h2range:NSMakeRange(5,1)];
[data getBytes:&h3range:NSMakeRange(6,1)];
[data getBytes:&h4range:NSMakeRange(7,1)];
int h = (h1 <<24) + (h2 <<16) + (h3 <<8) + h4;
returnCGSizeMake(w, h);
}
returnCGSizeZero;
}
+(CGSize)getJPGImageSizeWithRequest:(NSMutableURLRequest*)request
{
[request setValue:@"bytes=0-209"forHTTPHeaderField:@"Range"];
NSData* data = [NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:nil];
if ([datalength] <=0x58) {
returnCGSizeZero;
}
if ([datalength] <210) {//肯定只有一個DQT欄位
short w1 =0, w2 =0;
[data getBytes:&w1range:NSMakeRange(0x60,0x1)];
[data getBytes:&w2range:NSMakeRange(0x61,0x1)];
short w = (w1 <<8) + w2;
short h1 =0, h2 =0;
[data getBytes:&h1range:NSMakeRange(0x5e,0x1)];
[data getBytes:&h2range:NSMakeRange(0x5f,0x1)];
short h = (h1 <<8) + h2;
returnCGSizeMake(w, h);
} else {
short word =0x0;
[data getBytes:&wordrange:NSMakeRange(0x15,0x1)];
if (word ==0xdb) {
[data getBytes:&wordrange:NSMakeRange(0x5a,0x1)];
if (word ==0xdb) {//兩個DQT欄位
short w1 =0, w2 =0;
[data getBytes:&w1range:NSMakeRange(0xa5,0x1)];
[data getBytes:&w2range:NSMakeRange(0xa6,0x1)];
short w = (w1 <<8) + w2;
short h1 =0, h2 =0;
[data getBytes:&h1range:NSMakeRange(0xa3,0x1)];
[data getBytes:&h2range:NSMakeRange(0xa4,0x1)];
short h = (h1 <<8) + h2;
returnCGSizeMake(w, h);
} else {//一個DQT欄位
short w1 =0, w2 =0;
[data getBytes:&w1range:NSMakeRange(0x60,0x1)];
[data getBytes:&w2range:NSMakeRange(0x61,0x1)];
short w = (w1 <<8) + w2;
short h1 =0, h2 =0;
[data getBytes:&h1range:NSMakeRange(0x5e,0x1)];
[data getBytes:&h2range:NSMakeRange(0x5f,0x1)];
short h = (h1 <<8) + h2;
returnCGSizeMake(w, h);
}
} else {
returnCGSizeZero;
}
}
}
// 4、+(CGFloat)returnItemImageHeight:(NSString *)cellEditPath{
__blockCGFloat itemW =MSWIDTH-30;
__blockCGFloat itemH =0;
if (cellEditPath.length>0) {
UIImageView * imageView = [[UIImageViewalloc]init];
NSURL * url = [NSURLURLWithString:cellEditPath];
[imageView sd_setImageWithURL:urlplaceholderImage:[UIImageimageNamed:@"bg2"]];
SDWebImageManager *manager = [SDWebImageManagersharedManager];
BOOL existBool = [managerdiskImageExistsForURL:url];//判斷是否有快取
UIImage * image;
if (existBool) {
image = [[manager imageCache]imageFromDiskCacheForKey:url.absoluteString];
}else{
NSData *data = [NSDatadataWithContentsOfURL:url];
image = [UIImageimageWithData:data];
}
//根據image的比例來設定高度
if (image.size.width) {
itemH = image.size.height / image.size.width * itemW;
if (itemH >= itemW) {
itemH = image.size.height / image.size.width * itemW;
}
}
}
return itemH;
}
有沒有你喜歡的呢?????????