ios – 使用SDWebImage下载大图像时应用程序崩溃?

ios – 使用SDWebImage下载大图像时应用程序崩溃?,第1张

概述我正在使用SDWeb Image从服务器下载图像异步.服务器上的一些图像大小为1.5到2 MB.我在UICollectionView上显示这些图像.我在几次运行后得到了内存警告和应用程序崩溃.有时它会在第一次下载图像时出现,有时我会上下滚动集合视图. 这是我的代码 – -(void)setImageWithUrl:(NSURL*)imgurl onImageView:(UIImageView*)i 我正在使用SDWeb Image从服务器下载图像异步.服务器上的一些图像大小为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;     }];}

在AppDelegate-

- (voID)applicationDIDReceiveMemoryWarning:(UIApplication *)application{    [[SDImageCache sharedImageCache] clearMemory];    [[SDImageCache sharedImageCache] cleandisk];    [[SDImageCache sharedImageCache] cleardisk];    [[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];}

请不要图像是高分辨率(超过3000像素宽度).例如其中一个有4256 * 2832和979KB大小产生内存警告.任何帮助或建议将不胜感激.
编辑: – 我的应用程序因内存压力而被杀,但仅限于iPhone 4s及更低版本(在iPhone5及更高版本上工作正常).当下载大型图像(或完整下载)时,我突然出现内存峰值(它从〜约25mb到~90mb)和应用程序崩溃.任何建议怎么办?

解决方法 听起来你的应用程序因内存压力而被杀死.检查 Instruments确切内存增加的位置,并尝试使用 Autorelease Pool来缓解任何内存峰值. 总结

以上是内存溢出为你收集整理的ios – 使用SDWebImage下载大图像时应用程序崩溃?全部内容,希望文章能够帮你解决ios – 使用SDWebImage下载大图像时应用程序崩溃?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1060911.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存