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

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

问题描述

这个问题让我完全不知所措。这适用于带有Xcode 4.2的iOS 5.0

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然后我为其中一个设置图像视图的图像用户从apps dir中选择的图片。问题是在经过一定数量的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,但最后仍然崩溃。我不能发布或dealloc,因为我在iOS 5.0上使用Xcode 4.2运行它

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 缩小版本。 c>call。

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天全站免登陆