zoukankan      html  css  js  c++  java
  • 根据图片的链接获取图片的宽高

    //根据图片的链接获取图片的宽高
    - (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
  • 相关阅读:
    Vue生命周期,及父子组件生命周期顺序
    使用jquery制作可视化的组织结构
    用Moon.Orm来做分页数据显示
    bash脚本之代码统计
    CSS选择符总结
    css选择符归类
    APP测试与WEB测试的区别
    使用Jmeter 对APP进行压力测试
    Python基础之数据类型
    App测试要点以及Bug分类
  • 原文地址:https://www.cnblogs.com/hw140430/p/4189436.html
Copyright © 2011-2022 走看看