簡體   English   中英

使用SDWebImage下載大圖像時應用程序崩潰?

[英]App crash while downloading large images using SDWebImage?

我正在使用SDWebImage從服務器下載圖像異步。服務器上的一些圖像大小為1.5到2 UICollectionView 。我在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)和應用程序崩潰。任何建議怎么辦?

聽起來你的應用程序因內存壓力而被殺死。 檢查儀器內存增加的確切位置,並嘗試使用自動釋放池來緩解任何內存峰值。

是的,@ dlinsin是絕對正確的。 這絕對是內存問題。

我希望你沒有使用相同的2mb圖像作為縮略圖。 如果是,那就是你崩潰的原因。 請將較低分辨率的圖像用作拇指,並在用戶要求時顯示較大的圖像

順便說一句,用它來看看是否有任何幫助。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM