【问题标题】:SDWebImage's sd_setImageWithURL crashes the application when trying to show 100 of images in viewSDWebImage 的 sd_setImageWithURL 在尝试在视图中显示 100 个图像时使应用程序崩溃
【发布时间】:2015-06-04 13:04:13
【问题描述】:

我在我的一个屏幕上使用 SDWebImage 异步显示大约 100 个网址。

当我尝试显示来自 url 的所有图像时,我遇到了内存负载问题。

我有一个 UIScrollView 并且我有一个 UIImageView 动态添加了所有 100 个 Uibutton。

现在我的要求是,当用户在特定级别放大 UIScrollView 时,我需要在 UIbutton 中显示图像,当我开始使用以下代码将图像从服务器显示到该按钮时,它会崩溃并出现内存警告。

[btn sd_setImageWithURL:[NSURL URLWithString:busOwner.WhiteLargeLogoURL] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:placeHolderImage]];

谁能告诉我,我该如何解决这个内存问题,并且还可以同时向所有 100 个按钮显示图像?

我也尝试过使用 uiiamgeView 而不是 uiButton,我也尝试过使用

 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
        dispatch_async(queue, ^{
            // Perform non main thread operation
            for (BusinessOwnerEntity *busOwner in arrBusOwner) {
                NSURL *url = [NSURL URLWithString:busOwner.WhiteLargeLogoURL];

                SDWebImageManager *manager = [SDWebImageManager sharedManager];

                [manager downloadImageWithURL:url options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {

                    NSLog(@"REcieved File %ld",(long)receivedSize);

                } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {

                }];
            }
            dispatch_sync(dispatch_get_main_queue(), ^{
                // perform main thread operation
            });
        });

之前下载图片,但这里也崩溃了..

【问题讨论】:

    标签: objective-c memory-leaks ios8 sdwebimage didreceivememorywarning


    【解决方案1】:

    使用SDWebImage 回调将图像加载到队列中,而不是同时加载。
    它将减少您的内存消耗并避免 iOS 杀死您的应用程序。

    [manager downloadImageWithURL:url options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
    
                    NSLog(@"REcieved File %ld",(long)receivedSize);
    
                } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                           if ("are they less than 10 images loading?"){
                              [self startNewImageLoading];
                           }
                }];
    

    【讨论】:

    • 会用你的逻辑检查它并让你知道..谢谢你的帮助
    • 它在某种程度上减少了内存,但没有那么多......当我尝试将图像保存在具有巨大内存负载的文档目录中时它仍然崩溃
    • 减少队列的大小。也适用于本地储蓄。目标是在 iOS 上保持低调。确保只加载实际在屏幕上或即将出现的图像。如果在只能显示 10 个图像时分配了 100 个图像,则存在严重的性能问题。尝试检测您的图像何时将出现并加载它。并在它离开屏幕时(在本地缓存后)将其无效。它将使用 i/o 和 CPU,但会减少 RAM 使用量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多