简体   繁体   English

Objective-c UIImage 存储在 MSMutableArray 替代方案中,潜在的内存泄漏?

[英]Objective-c UIImage storage in MSMutableArray alternative, potential memory-leaks?

I am building an app for iOS 5 using ARC and I seem to be having some memory issues.我正在使用 ARC 为 iOS 5 构建应用程序,我似乎遇到了一些 memory 问题。 Basically its taking screen-shots of a portion of the display, placing the UIImage in an MSMutableArray and then piecing the screen-shots together for one big image.基本上它会截取显示的一部分的屏幕截图,将 UIImage 放在 MSMutableArray 中,然后将屏幕截图拼凑成一个大图像。 Now the problem is that after doing this a couple of times the OS closes the application due to high memory usage.现在的问题是,在执行此操作几次后,由于 memory 使用率较高,操作系统关闭了应用程序。

Here is the snippet that pieces the UIImage's together:这是将 UIImage 拼凑在一起的片段:

UIImage* finalImage = nil;
//join the screenshot images together
UIGraphicsBeginImageContext(CGSizeMake(collage.width, collage.height));
{
    int hc = 0;
    for(UIImage *img in imageArr)
    {
        NSLog(@"drawing image at:: %i", hc);
        [img drawAtPoint:CGPointMake(0, hc)];
        hc+=img.size.height;
        img = nil;
    }

    //NSLog(@"creating finalImage");
    finalImage = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();
//do something with the combined image
//remove all the objects
[imageArr removeAllObjects];
//reset class instance
[self setImageArr: [[NSMutableArray alloc] init]];

Are they any other alternatives that I could use so there isn't so much memory being used?他们是否还有其他我可以使用的替代品,所以没有那么多 memory 被使用? Maybe storing a CGImageRef in the array?也许在数组中存储 CGImageRef? Are there any potential memory leaks with the above code?上述代码是否存在潜在的 memory 泄漏?

Any tips, pointers would be greatly appreciated.任何提示,指针将不胜感激。

Thanks.谢谢。

[imageArr removeAllObjects]; will remove the objects from array.将从数组中删除对象。 No need to reset the array again with无需再次重置阵列

 [self setImageArr: [[NSMutableArray alloc] init]];

By doing this you are allocating a NSMutableArray object and not releasing it.通过这样做,您正在分配一个 NSMutableArray object 而不是释放它。

Try by removing the line [self setImageArr: [[NSMutableArray alloc] init]];尝试删除行[self setImageArr: [[NSMutableArray alloc] init]];

make sure you alloc and init setImageArr确保你分配并初始化 setImageArr

if (setImageArr == nil){
setImageArr = [[NSMutableArray alloc]init];
}
else
{
[setImageArr removeAllObjects];
}

or use (if you want to init from an existing Array):或使用(如果你想从现有数组初始化):

NSMutableArray *setImageArr = [[NSMtableArray]initWithArray:arrayOfImages];

Because you say it will have memory issue after doing this a couple of times .因为你说这样doing this a couple of times后会有 memory 问题。 Then how about you use NSAutoreleasePool to force system release objects after your method, example below:那么你如何使用NSAutoreleasePool在你的方法之后强制系统释放对象,下面的例子:

@autoreleasepool {
    UIImage* finalImage = nil;
    //join the screenshot images together
    UIGraphicsBeginImageContext(CGSizeMake(collage.width, collage.height));
    {
        int hc = 0;
        for(UIImage *img in imageArr)
        {
            NSLog(@"drawing image at:: %i", hc);
            [img drawAtPoint:CGPointMake(0, hc)];
            hc+=img.size.height;
            img = nil;
        }
        finalImage = UIGraphicsGetImageFromCurrentImageContext();
    }
    UIGraphicsEndImageContext();
    //do something with the combined image
    //remove all the objects
    [imageArr removeAllObjects];
    //reset class instance
    [self setImageArr: [[NSMutableArray alloc] init]];
}

And I also doubt there is any memory leak problem in your other codes.而且我也怀疑您的其他代码中是否存在任何 memory 泄漏问题。 Using ARC doesn't meaning without memory leak problem, maybe you store many useless objects in a global variable etc.使用 ARC 并不意味着没有 memory 泄漏问题,也许您将许多无用的对象存储在全局变量等中。

Maybe you should use Instruments to monitor the memory to figure out where does the memory go.也许你应该使用 Instruments 来监控 memory 来找出 memory go 在哪里。

Turns out the imageArr is being cleared properly.原来 imageArr 被正确清除了。 There appears to be a memory issue somewhere else in the program.程序中的其他地方似乎存在 memory 问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM