草庐IT

ios - AFNetworking 低内存问题

coder 2024-01-19 原文

我使用这段代码下载图片:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SECustomCollectionViewCell *collectionViewCell = (SECustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"SECustomCollectionViewCell" forIndexPath:indexPath];

    NSDictionary *artwork = [self.artworks objectAtIndex:indexPath.item];

    collectionViewCell.theImageView.image = nil;

    if (artwork[@"video_url"])
    {
        UIWebView *webView = (UIWebView *)[collectionViewCell.contentView viewWithTag:100];
        NSString * html = [self embedYouTube:artwork[@"video_url"] frame:collectionViewCell.frame];

        [webView setHidden:NO];

        [webView loadHTMLString:html baseURL:nil];

        [collectionViewCell.activityIndicator setHidden:YES];
    }
    else
    {
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:artwork[@"image_url"]]];

        UIImage *cachedImage = [[[UIImageView class] sharedImageCache] cachedImageForRequest:request];

        if (cachedImage)
        {
            collectionViewCell.theImageView.image = [UIImage scaleImage:cachedImage toWidth:collectionViewCell.frame.size.width];
            [collectionViewCell.activityIndicator setHidden:YES];
        }
        else
        {
            [collectionViewCell.theImageView setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

                // Only update visible cell, to avoid inserting image to another cell.
                SECustomCollectionViewCell *visibleCollectionViewCell = (id)[collectionView cellForItemAtIndexPath:indexPath];

                if (visibleCollectionViewCell)
                {
                    [visibleCollectionViewCell.theImageView setImage:[UIImage scaleImage:image toWidth:collectionViewCell.frame.size.width]];

                    [visibleCollectionViewCell.activityIndicator stopAnimating];

                    [visibleCollectionViewCell.activityIndicator setHidden:YES];
                }

            } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {

            }];
        }
    }

    return collectionViewCell;
}

但它会导致内存不足的问题。

最佳答案

导致内存使用的不是 AFNetworking,而是图像代码。 JPEG 压缩图像,但创建图像时每个像素将有 4 个字节。由于服务器上的 jpeg 文件大小为 1.4MB,因此所有 AFNetworking 都将加载。

看起来您正在使用一些帮助程序类查看该代码和 NSLog AFNetworking 下载的实际数据的大小。

每像素 4 字节的 5407∆×∆3605 图像将创建超过 77MB 的图像。您可以缩放它,但首先要渲染原始图像,缩放会使用更多内存,因为最后您将有两个图像。

您需要将原始图像的创建和缩放包装在自动释放池中,以便尽快释放原始图像。

最好不要一开始就加载这么大的图片。

关于ios - AFNetworking 低内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27637203/

有关ios - AFNetworking 低内存问题的更多相关文章

  1. ruby-on-rails - Ruby net/ldap 模块中的内存泄漏 - 2

    作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代

  2. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  3. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  4. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  5. ruby - Fast-stemmer 安装问题 - 2

    由于fast-stemmer的问题,我很难安装我想要的任何ruby​​gem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=

  6. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  7. ruby-on-rails - Ruby 中的内存模型 - 2

    ruby如何管理内存。例如:如果我们在执行过程中采用C程序,则以下是内存模型。类似于这个ruby如何处理内存。C:__________________|||stack|||------------------||||------------------|||||Heap|||||__________________|||data|__________________|text|__________________Ruby:? 最佳答案 Ruby中没有“内存”这样的东西。Class#allocate分配一个对象并返回该对象。这就是程序

  8. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  9. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  10. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

随机推荐