草庐IT

ios - 与 assetsd 的连接中断或 assetsd 死亡(带有内存警告)

coder 2024-01-12 原文

我们正在尝试让用户从他们的相册中导入图片(UIImagePickerController),并且我们正在缩放/调整大于 8 兆像素(iPhone 标准)的图像。

但每次应用程序崩溃时,与 assetsd 的连接中断或 assetsd 死亡Received memory warning 在导入图片之后或之前发出警告。有时 Received memory warning 仍在UIImagePickerController中寻找要导入的图片时弹出警告。

特别是在 iPhone 4S 上,情况更糟,请帮助我们优化我们的代码,使其在 iPhone 4S 或 iPad 2 等旧设备上运行时没有警告和崩溃。

如果我们在使用 CoreGraphics 缩小/调整图像大小时做错了什么,请告诉我们。(因为这是使用大量内存的地方)。

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        UIImage *selectedImage=[info objectForKey:UIImagePickerControllerOriginalImage];
        if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
        {
            [picker dismissViewControllerAnimated:YES completion:nil];
        }
        else
        {
            [popoverController dismissPopoverAnimated:YES];
            [self popoverControllerDidDismissPopover:popoverController];
        }

        // COMPRESSING IMAGE
        NSData   *selectedImageData=UIImageJPEGRepresentation(selectedImage, 0.1);
        UIImage *selectedImageFromData=[UIImage imageWithData:selectedImageData];

        // IMAGE ASPECT RATIO
        CGFloat originalWidth=selectedImageFromData.size.width;
        CGFloat originalHeight=selectedImageFromData.size.height;
        CGFloat myWidth=2048;
        CGFloat myHeight=2048;
        CGFloat widthRatio=myWidth/originalWidth;
        CGFloat heightRatio=myHeight/originalHeight;
        CGFloat dynamicWidth=heightRatio*originalWidth;
        CGFloat dynamicHeight=widthRatio*originalHeight;


        //SCALING UIIMAGE MORE THAN 8 MEGAPIXELS
        if (((selectedImageFromData.size.width>3264) && (selectedImageFromData.size.height>2448)) || ((selectedImageFromData.size.height>3264) && (selectedImageFromData.size.width>2448)))
        {



             // DATA FROM UIIMAGE TO CORE GRAPHICS
             CGImageRef CoreGraphicsImage=selectedImageFromData.CGImage;
            CGColorSpaceRef colorSpace = CGImageGetColorSpace(CoreGraphicsImage);
            CGBitmapInfo bitmapInfo=CGImageGetBitmapInfo(CoreGraphicsImage);
            CGImageGetBitsPerComponent(CoreGraphicsImage);


            // RESIZING WIDTH OF THE IMAGE
            if (originalWidth>originalHeight)
            {


            CGContextRef context=CGBitmapContextCreate(NULL, myWidth, dynamicHeight, CGImageGetBitsPerComponent(CoreGraphicsImage), CGImageGetBytesPerRow(CoreGraphicsImage), colorSpace, bitmapInfo);


            CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
            CGContextDrawImage(context, CGRectMake(0, 0, myWidth, dynamicHeight), CoreGraphicsImage);
            CGImageRef CGscaledImage=CGBitmapContextCreateImage(context);
                UIImage *CGLastimage = [UIImage imageWithCGImage: CGscaledImage];
                NSLog(@"%f",CGLastimage.size.width);
                NSLog(@"%f",CGLastimage.size.height);

                VisualEffectImageVIew.image=CGLastimage;
                BackgroundImageView.image=CGLastimage;
                ForegroundImageView.image=CGLastimage;
            }


            //RESIZING HEIGHT OF THE IMAGE
            if (originalHeight>originalWidth)
            {
                CGContextRef context=CGBitmapContextCreate(NULL, dynamicWidth, myHeight, CGImageGetBitsPerComponent(CoreGraphicsImage), CGImageGetBytesPerRow(CoreGraphicsImage), colorSpace, bitmapInfo);


                CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
                CGContextDrawImage(context, CGRectMake(0, 0, dynamicWidth, myHeight), CoreGraphicsImage);
                CGImageRef CGscaledImage=CGBitmapContextCreateImage(context);
                UIImage *CGLastimage = [UIImage imageWithCGImage: CGscaledImage];

                NSLog(@"%f",CGLastimage.size.width);
                NSLog(@"%f",CGLastimage.size.height);

                VisualEffectImageVIew.image=CGLastimage;
                BackgroundImageView.image=CGLastimage;
                ForegroundImageView.image=CGLastimage;

            }


        }
        else
        {
            NSLog(@" HEIGHT %f",selectedImageFromData.size.height);
            NSLog(@" WIDTH %f",selectedImageFromData.size.width);

        VisualEffectImageVIew.image=selectedImageFromData;
        BackgroundImageView.image=selectedImageFromData;
        ForegroundImageView.image=selectedImageFromData;
        }


    }

内存报告

在 UIImagePickerController 中滚动时

http://i.stack.imgur.com/qxx62.png

缩放/调整 UIImage 大小时

http://i.stack.imgur.com/ELCA6.png

最佳答案

两点。

首先,您没有释放您创建的对象——您正在泄漏大量内存。无论您是否使用 ARC,都必须为每个 CGCreate 调用适本地调用 CGContextRelease 或 CGImageRelease。

其次,如果您只想调整大小,使用 coregraphics 就太过分了,请改用 UIKit。在我下面使用的代码中,请注意 @autorelease 的使用,以确保一旦上下文结束,ARC 就会清除对象

- (UIImage *)fitImage:(UIImage *)image scaledToFillSize:(CGSize)size {
    // do not upscale

    @autoreleasepool {
        if( image.size.width <= size.width && image.size.height <= size.height )
            return image;

        CGFloat scale = MIN(size.width/image.size.width, size.height/image.size.height);
        CGFloat width = image.size.width * scale;
        CGFloat height = image.size.height * scale;

        CGRect imageRect;
        // center image
        if( scale != 1.0 ) { // avoid divide by zero?
            if( size.width/image.size.width < size.height/image.size.height ) {
                // height needs to be centered
                imageRect = CGRectMake(0, (size.height-height)/2, width, height);
            } else {
                // width needs to be centered
                imageRect = CGRectMake((size.width-width)/2, 0, width, height);
            }
        }

        UIGraphicsBeginImageContextWithOptions(size, YES, 0);
        [[UIColor CollageBorderUIColor] setFill]; // otherwise it's ugly black fill
        UIRectFill(CGRectMake(0, 0, size.width, size.height));
        [image drawInRect:imageRect];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }
}

关于ios - 与 assetsd 的连接中断或 assetsd 死亡(带有内存警告),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30137378/

有关ios - 与 assetsd 的连接中断或 assetsd 死亡(带有内存警告)的更多相关文章

  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 - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  4. 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

  5. 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

  6. ruby - 无法在 60 秒内获得稳定的 Firefox 连接 (127.0.0.1 :7055) - 2

    我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类

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

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

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

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

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

  10. 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

随机推荐