在 setimage 上收到内存警告 [英] Received memory warning on setimage

查看:16
本文介绍了在 setimage 上收到内存警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题把我彻底难住了.这是针对 iOS 5.0 和 Xcode 4.2

This problem has completely stumped me. This is for iOS 5.0 with Xcode 4.2

发生的事情是,在我的应用程序中,我让用户从他们的相册中选择图像,然后将这些图像保存到应用程序文档目录中.非常直接.

What's going on is that in my app I let user select images from their photo album and I save those images to apps document directory. Pretty straight forward.

然后我要做的是在其中一个 viewController.m 文件中创建多个 UIImageViews,然后从用户从应用程序目录中选择的一张图片中设置图像视图的图像.问题是在设置一定数量的 UIImage 后,我收到收到的内存警告".它通常发生在有 10 张图片时.如果假设用户选择了 11 张图片,则应用程序会因错误 (GBC) 而崩溃.注意:这些图像中的每一个都至少为 2.5 MB.

What I do then is that in one of the viewController.m files I create multiple UIImageViews and I then set the image for the image view from one of the picture that user selected from apps dir. The problem is that after a certain number of UIImage sets I receive a "Received memory warning". It usually happens when there are 10 pictures. If lets say user selected 11 pictures then the app crashes with Error (GBC). NOTE: each of these images are at least 2.5 MB a piece.

经过几个小时的测试,我终于将问题缩小到这行代码

After hours of testing I finally narrowed down the problem to this line of code

[button1AImgVw setImage:image];

如果我注释掉该代码.所有编译都很好,没有发生内存错误.但是如果我不注释掉该代码,我会收到内存错误并最终崩溃.另请注意,它确实处理了整个 CreateViews IBAction,但最终仍会崩溃.由于我在 iOS 5.0 和 Xcode 4.2 上运行它,所以我无法执行 release 或 dealloc

If I comment out that code. All compiles fine and no memory errors happen. But if I don't comment out that code I receive memory errors and eventually a crash. Also note it does process the whole CreateViews IBAction but still crashes at the end. I cannot do release or dealloc since I am running this on iOS 5.0 with Xcode 4.2

这是我使用的代码.谁能告诉我我做错了什么?

Here is the code that I used. Can anyone tell me what did I do wrong?

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self CreateViews];
}

-(IBAction) CreateViews
{
    paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES);
    documentsPath = [paths objectAtIndex:0]; 

    //here 15 is for testing purposes    
    for (int i = 0; i < 15; i++) 
    {    
        //Lets not get bogged down here. The problem is not here
        UIImageView *button1AImgVw = [[UIImageView alloc] initWithFrame:CGRectMake(10*i, 10, 10, 10)];
        [self.view addSubview:button1AImgVw];

        NSMutableString *picStr1a = [[NSMutableString alloc] init];
        NSString *dataFile1a = [[NSString alloc] init];

        picStr1a = [NSMutableString stringWithFormat:@"%d.jpg", i];
        dataFile1a = [documentsPath stringByAppendingPathComponent:picStr1a];
        NSData *potraitImgData1a =[[NSData alloc] initWithContentsOfFile:dataFile1a];
        UIImage *image = [[UIImage alloc] initWithData:potraitImgData1a];

        // This is causing my app to crash if I load more than 10 images!
    //  [button1AImgVw setImage:image];

//If I change this code to a static image. That works too without any memory problem.
button1AImgVw.image = [UIImage imageNamed:@"mark-yes.png"]; // this image is less than 100KB
        }

        NSLog(@"It went to END!");

    }

这是我在选择 10 个图像时得到的错误.应用启动并运行

This is the error I get when 10 images are selected. App does launch and work

2012-10-07 17:12:51.483 ABC-APP[7548:707] It went to END!
2012-10-07 17:12:51.483 ABC-APP [7531:707] Received memory warning.

当有 11 张图片时,应用程序崩溃并出现此错误

App crashes with this error when there are 11 images

2012-10-07 17:30:26.339 ABC-APP[7548:707] It went to END!
(gbc)

推荐答案

这种情况(尝试将多个全分辨率 UIImages 加载到视图中时出现内存警告和应用程序退出)在我的 iOS 编程生涯中曾多次试图让我感到痛苦.

This situation (memory warnings and application quitting while attempting to load multiple full resolution UIImages into a view) has attempted to burn me a couple times in my iOS programming career.

在执行setImage"调用之前,您需要制作一个缩小原始图像的副本.

You need to make a shrinked down copy of your original image before doing the "setImage" call.

对于我自己的代码,我使用UIImage+Resize"类别,可在此处找到详细信息.

For my own code, I use the "UIImage+Resize" category, the details for which can be found here.

在插入视图之前将图像调整为较小的大小,然后确保发布全分辨率图像(如果在 ARC 上设置为 nil),那么您应该有更快乐的时光.

Resize your image to something smaller before inserting into your view, then make sure the full resolution image is released (or set to nil if on ARC) and you should have a happier time of things.

这是我在自己的代码中如何做到的:

Here is how I do it in my own code:

CGSize buttonSize = CGSizeMake(width, height);
// it'd be nice if UIImage took a file URL, huh?
UIImage * newImage = [[UIImage alloc] initWithContentsOfFile: pathToImage];
if(newImage)
{
    // this "resizedimage" image is what you want to pass to setImage
    UIImage * resizedImage = [newImage resizedImage: buttonSize interpolationQuality: kCGInterpolationLow];
}

这篇关于在 setimage 上收到内存警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆