• SDWebImage 内存优化


    SDWebImage大家肯定都恨熟悉了,国内外太多的App使用其进行图片加载。

    但是最近在使用过程中发现,我用SDWebImage加载多个图片,类似微博动态那种,在加载的过程中。我发现当图片分辨率比较大的时候(不是图片大),加载几张图片就崩溃了。

    网上说可以每次加载图片清空memcache,但是效果并不好。[[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];

     

    也有说把使用下面这个方法的地方全部注掉

     

    + (UIImage *)decodedImageWithImage:(UIImage *)image 

    但是效果并不明显。同时加载5-7张高分辨率图片还是会立即崩溃

    我们使用SDWebimage肯定都会做三件事,一判断本地是否有这张图,二有的时候直接从本地取图片,三没有的时候去网络下载。

    大概是像下面这样

     

    [objc]
    1. NSString *logoString = [_currentDic stringValueForKey:@"team_img"];  
    2.   
    3.    if(logoString.length>0){  
    4.    [[SDImageCache sharedImageCache] queryDiskCacheForKey:logoString done:^(UIImage *image, SDImageCacheType cacheType) {  
    5.        if (image) {  
    6.            [_teamImage setImage:image];  
    7.        }else{  
    8.            [_teamImage sd_setImageWithURL:kNSUrl(logoString)  
    9.                          placeholderImage:IMGNAMED(@"defaultAvatar2")  
    10.                                   options:SDWebImageRefreshCached  
    11.                                 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {  
    12.                                     if (image) {  
    13.                                         [[SDImageCache sharedImageCache] storeImage:image forKey:logoString toDisk:YES];  
    14.                                     }  
    15.                                 }];  
    16.        }  
    17.        }];}  


    在内部都会使用到下面这个方法

    [objc]
    1. - (UIImage *)diskImageForKey:(NSString *)key {  
    2.     NSData *data = [self diskImageDataBySearchingAllPathsForKey:key];  
    3.     if (data) {  
    4.         UIImage *image = [UIImage sd_imageWithData:data];  
    5.         image = [self scaledImageForKey:key image:image];  
    6.         image = [UIImage decodedImageWithImage:image];  
    7.         return image;  
    8.     }  
    9.     else {  
    10.         return nil;  
    11.     }  
    12. }  


    我发现这里

    1. UIImage *image = [UIImage sd_imageWithData:data];  

    图片取出来的时候就已经巨大无比,占用了很大的内存,导致内存来不及释放就崩溃。

    抽丝剥茧我们进入

    1. sd_imageWithData方法  

    发现这里面对图片的处理是直接按照原大小进行的,如果几千是分辨率这里导致占用了大量内存。


    所以我们需要在这里对图片做一次等比的压缩。

    我们在

    UIImage+MultiFormat这个类里面添加如下压缩方法,

    1. +(UIImage *)compressImageWith:(UIImage *)image  
    2. {  
    3.     float imageWidth = image.size.width;  
    4.     float imageHeight = image.size.height;  
    5.     float width = 640;  
    6.     float height = image.size.height/(image.size.width/width);  
    7.       
    8.     float widthScale = imageWidth /width;  
    9.     float heightScale = imageHeight /height;  
    10.       
    11.     // 创建一个bitmap的context  
    12.     // 并把它设置成为当前正在使用的context  
    13.     UIGraphicsBeginImageContext(CGSizeMake(width, height));  
    14.       
    15.     if (widthScale > heightScale) {  
    16.         [image drawInRect:CGRectMake(0, 0, imageWidth /heightScale , height)];  
    17.     }  
    18.     else {  
    19.         [image drawInRect:CGRectMake(0, 0, width , imageHeight /widthScale)];  
    20.     }  
    21.       
    22.     // 从当前context中创建一个改变大小后的图片  
    23.     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();  
    24.     // 使当前的context出堆栈  
    25.     UIGraphicsEndImageContext();  
    26.       
    27.     return newImage;  
    28.       
    29. }  

    再在上面箭头代码后面对图片进行压缩

     

    1. #ifdef SD_WEBP  
    2.     else if ([imageContentType isEqualToString:@"image/webp"])  
    3.     {  
    4.         image = [UIImage sd_imageWithWebPData:data];  
    5.     }  
    6. #endif  
    7.     else {  
    8.         image = [[UIImage alloc] initWithData:data];  
    9.         if (data.length/1024 > 128) {  
    10.             image = [self compressImageWith:image];  
    11.         }  
    12.         UIImageOrientation orientation = [self sd_imageOrientationFromImageData:data];  
    13.         if (orientation != UIImageOrientationUp) {  
    14.             image = [UIImage imageWithCGImage:image.CGImage  
    15.                                         scale:image.scale  
    16.                                   orientation:orientation];  
    17.         }  

    到了这里还需要进行最后一步。就是在SDWebImageDownloaderOperation的connectionDidFinishLoading方法里面的:


    UIImage *image = [UIImage sd_imageWithData:self.imageData];

    //将等比压缩过的image在赋在转成data赋给self.imageData
    NSData *data = UIImageJPEGRepresentation(image, 1);
    self.imageData = [NSMutableData dataWithData:data];

       再配合    [[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];(图片加载后使用)大功告成,亲测内存基本变化不大,自动释放也来得及。

  • 相关阅读:
    一位老司机开车20年后得到的26条教训
    H608B无线路由破解方法
    我们去工作到底为了什么?
    什么是”中国梦”?
    Cubieboard A10 安装Nand系统,配置nginx,php,mysql,samba详细教程
    早晚有一天,我们都会成为自己当初讨厌的人
    当下中国的十二种孤独
    终于见到全文了,太唯美了!
    想生男想生女 从科学角度为你解读
    如何找到板块中所有个股
  • 原文地址:https://www.cnblogs.com/pioneerMax/p/6374093.html
Copyright © 2020-2023  润新知