加载高清大图崩溃问题

 

SDWebImage加载高清大图崩溃问题:html

经验证没测试出来,在网上查找根源应该是在iOS7上有问题,特此记录一下ios

 第一种:老版本SDWebImage_v4.2.0git

 更改源码github

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

一、在UIImage+MultiFormat 中增长方法,对图片作一次等比的压缩。安全

+(UIImage *)compressImageWith:(UIImage *)image
{
    float imageWidth = image.size.width;
    float imageHeight = image.size.height;
    float width = 640;
    float height = image.size.height/(image.size.width/width);
    
    float widthScale = imageWidth /width;
    float heightScale = imageHeight /height;
    
    // 建立一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(CGSizeMake(width, height));
    
    if (widthScale > heightScale) {
        [image drawInRect:CGRectMake(0, 0, imageWidth /heightScale , height)];
    }
    else {
        [image drawInRect:CGRectMake(0, 0, width , imageHeight /widthScale)];
    }
    
    // 从当前context中建立一个改变大小后的图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    
    return newImage;
    
}

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

        image = [[UIImage alloc] initWithData:data];
        if (data.length/1024 > 128) {
            image = [self compressImageWith:image];
        }

三、 在SDWebImageDownloaderOperation.m 的 -(void)connectionDidFinishLoading:(NSURLConnection *)aConnection 方法中增长代码async

-(void)connectionDidFinishLoading:(NSURLConnection *)aConnection {
    SDWebImageDownloaderCompletedBlock completionBlock = self.completedBlock;
    @synchronized(self) {
        CFRunLoopStop(CFRunLoopGetCurrent());
        self.thread = nil;
        self.connection = nil;
        dispatch_async(dispatch_get_main_queue(), ^{
            [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self];
            [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadFinishNotification object:self];
        });
    }
    
    if (![[NSURLCache sharedURLCache] cachedResponseForRequest:_request]) {
        responseFromCached = NO;
    }
    
    if (completionBlock) {
        if (self.options & SDWebImageDownloaderIgnoreCachedResponse && responseFromCached) {
            completionBlock(nil, nil, nil, YES);
        } else if (self.imageData) {
            UIImage *image = [UIImage sd_imageWithData:self.imageData];
//增长的代码
            NSData *data = UIImageJPEGRepresentation(image, 1);
            self.imageData = [NSMutableData dataWithData:data];
//
            NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];
            image = [self scaledImageForKey:key image:image];
            
            // Do not force decoding animated GIFs
            if (!image.images) {
                if (self.shouldDecompressImages) {
                    image = [UIImage decodedImageWithImage:image];
                }
            }
            if (CGSizeEqualToSize(image.size, CGSizeZero)) {
                completionBlock(nil, nil, [NSError errorWithDomain:SDWebImageErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey : @"Downloaded image has 0 pixels"}], YES);
            }
            else {
                completionBlock(image, self.imageData, nil, YES);
            }
        } else {
            completionBlock(nil, nil, [NSError errorWithDomain:SDWebImageErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey : @"Image data is nil"}], YES);
        }
    }
    self.completionBlock = nil;
    [self done];
}

 四、在控制器的- (void)didReceiveMemoryWarning方法中添加oop

    [[SDWebImageManager sharedManager] cancelAll];
    [[SDImageCache sharedImageCache] clearMemory];

 

 

第二种:新版本post

解压缩意义:

当完成图片加载或者从本地加载图片时,还会有轻微的卡顿。由于当显示或者绘制的时候,UIKit 只作了额外的延迟初始化和消耗很高解码。因此从后台线程解压缩成合适的格式,从而让系统没必要作额外的转换。而后在主线程上显示,增长流畅性。

 

优化为什么拔苗助长?最后在SDWebImage的issues找到了相关的讨论:
https://github.com/rs/SDWebImage/issues/538
其中一个harishkashyap大神是这么回答的:

harishkashyap commented on Dec 23, 2014
Its the memory issue again. decodedImageWithImage takes up huge memory and causes the app to crash. I have added an option to put this off in the library but defaulting to YES so there aren't any breaking changes. If you put off the decodeImageWithImage method in both image cache and image downloader then you shouldn't be seeing the VM: CG Raster data on the top consuming lots of memory

 

decodeImageWithImage is supposed to decompress images and cache them so the loading on tableviews/collectionviews become better. However, with large set of images being loaded, the experience worsened and the memory of uncompressed images even with thumbnails can consume GBs of memory. Putting this off only improved performance.

 

这位大神提到,decodeImageWithImage这个方法用于对图片进行解压缩而且缓存起来,以保证tableviews/collectionviews 交互更加流畅,可是若是是加载高分辨率图片的话,会拔苗助长,有可能形成上G的内存消耗。该大神建议,对于高分辨率的图片,应该在图片解压缩后,禁止缓存解压缩后的数据,相关的代码处理为:

[[SDImageCache sharedImageCache] setShouldDecompressImages:NO];
[[SDWebImageDownloader sharedDownloader] setShouldDecompressImages:NO];

固然,你也能够设置SDWebImage的其余参数,好比是否缓存到内存以及内存缓存最高限制等,来保证内存安全:

shouldCacheImagesInMemory 是否缓存到内存
maxMemoryCost  内存缓存最高限制

号外:苹果官方给出了一个下载高清大图的demo,内存消耗很低。感兴趣的朋友也能够看看:

https://developer.apple.com/library/ios/samplecode/LargeImageDownsizing/Introduction/Intro.html

 

CGBitmap两个参数
 
bitsPerComponent 表示存入内存中的每一个像素中的每个组件所占的位数;
bytesPerRow 表示存入内存中的位图的每一行所占的字节数;
      猜想,解压缩操做中,每个像素点都会分配一个空间来存储相关值,那么分辨率越高的图片,就意味着更多数量的像素点,也就意味着须要分配更多的空间!因此对于高分辨率图来讲,若是缓存解压缩以后的数据,即便是几M的图片,也是有可能消耗上G的内存!
既然如此,我决定按照harishkashyap大神的方法,直接让下载高分辨率图的地方,避免缓存解压缩后的数据操做!
 
摘自连接:https://www.jianshu.com/p/1c9de8dea3ea
相关文章
相关标签/搜索