草庐IT

ios - 在 dispatch_async 中收到内存警告

coder 2023-09-27 原文

这是我在 cellForRowAtIndex 中编写的用于下载图像的代码:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    @autoreleasepool {
        __block UIImage * img;
        __block NSData *data;
        if(![messageDocument.SmallImageURL isEqual:@""])
        {
            data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:messageDocument.SmallImageURL]];
            img = [UIImage imageWithData:data];
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            imgUser.image = img;
            img = nil;

            imgUser.contentMode = UIViewContentModeScaleAspectFill;
            CGSize size;
            if(imgUser.image.size.width > imageFrame.size.width || imgUser.image.size.height > rectImage.size.height)
            {
                if(imgUser.image.size.width < imageFrame.size.width)
                {
                    rectImage.size.width = imgUser.image.size.width;
                }

                if(imgUser.image.size.height < rectImage.size.height)
                {
                    rectImage.size.height = imgUser.image.size.height;
                }

                size = CGSizeAspectFit(imgUser.image.size, rectImage.size);
                imgUser.frame = CGRectMake(imgUser.frame.origin.x, rectImage.origin.y, size.width, size.height);

                height = imgUser.frame.size.height;
            }
            else
            {
                imgUser.frame = CGRectMake(imageFrame.origin.x, imageFrame.origin.y, imgUser.image.size.width, imgUser.image.size.height);
                height = imgUser.image.size.height;

            }

            CGPoint contentOffset = tableMessageDetail.contentOffset;
            [tableMessageDetail beginUpdates];
            [tableMessageDetail endUpdates];
            [tableMessageDetail setContentOffset:contentOffset];
        });

            messageDocument.Pic = data;
            data = nil;
        if(messageDocument.Pic != nil)
        {
            Attachment *attachment = [Attachment new];
            attachment.DocId = messageDocument.DocId;
            attachment.DocURL = messageDocument.DocURL;
            attachment.ImageId = messageDocument.ImageId;
            attachment.MessageId = messageDocument.MessageId;
            attachment.SmallImageURL = messageDocument.SmallImageURL;
            attachment.OriginalFileName = messageDocument.OriginalFileName;
            if([messageDocument.DocURL isEqual:@""])
            {
                NSArray *attachmentArray = [messageDocument.SmallImageURL componentsSeparatedByString:@"/"];
                NSString *attachmentName = [attachmentArray objectAtIndex:attachmentArray.count - 1];
                attachment.AttachmentName = attachmentName;
            }
            else
            {
                NSArray *attachmentArray = [messageDocument.DocURL componentsSeparatedByString:@"/"];
                NSString *attachmentName = [attachmentArray objectAtIndex:attachmentArray.count - 1];
                attachment.AttachmentName = attachmentName;
            }

            attachment.Pic = messageDocument.Pic;
            [[CommonModel shared]CreateAttachment:attachment];
            [[CommonModel shared]UpdateMessageDocumentPic:messageDocument];

            attachment = nil;
        }
    }
});  

但是如果有超过 6 张图片,我会在控制台上收到内存异常并显示以下消息:

Message from debugger: Terminated due to memory issue

最佳答案

你的下载逻辑没有问题。问题是您试图将图像保存在阵列中,这最终会增加您的应用程序内存。由于您持有的图像是压缩的 png,它们似乎没有太大的尺寸,但它会大大增加您的应用程序的内存并可能使您的应用程序崩溃。

要解决您的问题,您需要在下载每个文件后立即将其保存到文档目录,然后保存图像的名称(如果需要在某处显示,则保存缩略图)而不是整个完整分辨率图片。

更新

您可以使用 Xcode 中的 instruments 工具分析应用程序的内存分配。它可以为您提供消耗最多内存的对象的提示。

关于ios - 在 dispatch_async 中收到内存警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33991852/

有关ios - 在 dispatch_async 中收到内存警告的更多相关文章

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

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

  2. ruby - 在院子里用@param 标签警告 - 2

    我试图使用yard记录一些Ruby代码,尽管我所做的正是所描述的here或here#@param[Integer]thenumberoftrials(>=0)#@param[Float]successprobabilityineachtrialdefinitialize(n,p)#initialize...end虽然我仍然得到这个奇怪的错误@paramtaghasunknownparametername:the@paramtaghasunknownparametername:success然后生成的html看起来很奇怪。我称yard为:$yarddoc-mmarkdown我做错了什么?

  3. ruby-on-rails - active_admin 目录中的常量警告重新声明 - 2

    我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA

  4. ruby-on-rails - 启动 Rails 服务器时 ImageMagick 的警告 - 2

    最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru

  5. 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返回它复制的字节数,但是当我还没有下

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

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

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

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

  8. ruby-on-rails - 我更新了 ruby​​ gems,现在到处都收到解析树错误和弃用警告! - 2

    简而言之错误:NOTE:Gem::SourceIndex#add_specisdeprecated,useSpecification.add_spec.Itwillberemovedonorafter2011-11-01.Gem::SourceIndex#add_speccalledfrom/opt/local/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:91./opt/local/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails/gem_dependency.rb:275:in`==':und

  9. ruby-on-rails - 如何在发布新的 Ruby 或 Rails 版本时收到通知? - 2

    有人知道在发布新版本的Ruby和Rails时收到电子邮件的方法吗?他们有邮件列表,RubyonRails有一个推特,但我不想听到那些随之而来的喧嚣,我只想知道什么时候发布新版本,尤其是那些有安全修复的版本。 最佳答案 从therailsblog获取提要.http://weblog.rubyonrails.org/feed/atom.xml 关于ruby-on-rails-如何在发布新的Ruby或Rails版本时收到通知?,我们在StackOverflow上找到一个类似的问题:

  10. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

随机推荐