首页 > 其他 > 详细

根据图片的链接获取图片的宽高

时间:2014-12-28 00:20:57      阅读:389      评论:0      收藏:0      [点我收藏+]
//根据图片的链接获取图片的宽高
- (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

 

根据图片的链接获取图片的宽高

原文:http://www.cnblogs.com/hw140430/p/4189436.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!