使用SDWebImage下载大图像时,应用程序崩溃了吗?

Bha*_*rat 5 memory-management objective-c ios sdwebimage

SDWebImage用来从服务器上异步下载图像。服务器上的某些图像大小约为1.5至2 MB。我正在上显示这些图像UICollectionView。多次运行后,我收到内存警告并且应用程序崩溃。有时是在第一次下载图像时发生的,有时是在我上下滚动集合视图时发生的。这是我的代码-

-(void)setImageWithUrl:(NSURL*)imgurl onImageView:(UIImageView*)image prograssive:(BOOL)progressive
{
    __block UIActivityIndicatorView *activityIndicator;
    __weak UIImageView *weakImageView = image;
    SDWebImageOptions opt;
    if (progressive) {

        opt = SDWebImageProgressiveDownload;
    }
    else
        opt = SDWebImageRetryFailed;
    [image sd_setImageWithURL:imgurl placeholderImage:[UIImage imageNamed:@"default_image"] options:opt progress:^(NSInteger receivedSize, NSInteger expectedSize)
     {
         if (!activityIndicator)
         {
             [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
             activityIndicator.center = CGPointMake(weakImageView.frame.size.width /2, weakImageView.frame.size.height/2);
             // activityIndicator.center = weakImageView.center;
             [activityIndicator setBackgroundColor:[UIColor clearColor]];
             [activityIndicator startAnimating];
         }
     }
                    completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
     {
         [activityIndicator removeFromSuperview];
         activityIndicator = nil;
     }];
}
Run Code Online (Sandbox Code Playgroud)

并在AppDelegate-

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{

    [[SDImageCache sharedImageCache] clearMemory];
    [[SDImageCache sharedImageCache] cleanDisk];
    [[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];
}
Run Code Online (Sandbox Code Playgroud)

请不要使用高分辨率(超过3000像素宽度)的图像,例如其中一个图像大小为4256 * 2832,大小为979KB的图像会发出内存警告,请提供任何帮助或建议。 编辑:-我的应用程序由于内存压力而被杀死,但仅在iPhone 4s和更低版本上(在iPhone5及更高版本上可以正常工作)。当下载大图像(或完整下载)时,我突然出现内存峰值(它从〜25mb变为〜90mb),应用崩溃了。有什么建议可做吗?

dli*_*sin 3

听起来您的应用程序由于内存压力而被杀死。检查内存到底在哪里增加的仪器,也许尝试使用自动释放池来缓解任何内存峰值。