//根据图片的链接获取图片的宽高
- (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
CGSize size = CGSizeZero;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];
NSString *pathExtendsion = [URL.pathExtension lowercaseString];
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){
#ifdef dispatch_main_sync_safe
[[SDImageCache sharedImageCache] storeImage:image recalculateFromImage:YES imageData:data forKey:URL.absoluteString toDisk:YES];
#endif
size = image.size;
}
}
return size;
}
原帖地址:http://blog.csdn.net/li6185377/article/details/26225799