【问题标题】:App crash while downloading large images using SDWebImage使用 SDWebImage 下载大图像时应用程序崩溃
【发布时间】:2016-04-07 06:39:52
【问题描述】:

我正在使用 SDWebImage 从服务器异步下载图像。服务器上的一些图像大小(1772x1476 像素)。我在 UICollectionView 上显示这些图像。多次运行后,我收到内存警告和应用程序崩溃。有时会在第一次下载图像时发生,有时会在我上下滚动集合视图时发生。这是我的代码-

我的 x 代码收到警告,如下图所示

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

[Mainimage sd_setImageWithURL:[NSURL URLWithString: Obj.imageYH] placeholderImage:[UIImage imageNamed:@""]];

   return cell;
}

【问题讨论】:

  • 我坚信,这个错误不是因为图片下载造成的。
  • 那是什么原因呢?
  • 嗨,Krish,又是我。让我们做一些计算。您的 1772 x 1476 像素图像在下载过程中花费了相当多的 RAM。所用 RAM 为 1772 x 1476 x 24(位)/8 = 7846418 字节 = 7662 Kb = 7MB。由于设备的 RAM 有限,因此您不应加载如此巨大的照片以耗尽内存。考虑在服务器端调整图像大小,或者不要同时加载多张大照片。您可以在 Xcode 内存监视器中确认行为。
  • @Raptor 所以我必须通知服务器端的人调整图像大小吗?
  • 是的,否则您的应用会出现内存问题。更好的是,你也可以使用 SDWebImage 的下载管理器来一张一张的下载图片。

标签: ios objective-c memory-management sdwebimage


【解决方案1】:

你可以使用:

[SDImageCache sharedImageCache].config.shouldDecompressImages = NO; [[SDWebImageDownloader sharedDownloader] setShouldDecompressImages:NO];

在 4.0-beta 版中

【讨论】:

    【解决方案2】:

    如果您的应用不需要显示完整大小的图像,那么最好将其缩小然后显示。这是一个如何做的例子。

    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    [manager downloadWithURL:url delegate:self options:0 success:^(UIImage *image) 
    {
        //Scale down the image here and then set to your view.
    }
    

    它应该可以帮助您减少内存问题。

    【讨论】:

    • 你好 Ruchira Randana 你在吗?
    【解决方案3】:
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
                UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    
        [Mainimage setImageWithURL:[NSURL URLWithString: Obj.imageYH] placeholderImage:[UIImage imageNamed:@""]];
    
               return cell; 
    }        
    

    试试这个

    【讨论】:

    • 它也被贬低了
    • 你也可以试试这个 [imgView setImageWithURL:[self.imgArray objectAtIndex:index] placeholderImage:nil options:SDWebImageProgressiveDownload completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) { } usingActivityIndi​​catorStyle: UIActivityIndi​​catorViewStyleWhite];
    猜你喜欢
    • 2015-03-03
    • 2015-02-26
    • 2018-10-23
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    相关资源
    最近更新 更多