简体   繁体   中英

Application centered around UIImageView (Memory Leak)

I have an application that is centered around working through action-activated images. Most of the application would consist of multifarious variations of the following, which would be below.

-(IBAction)tap{
    if(a == 0){
        a = 1;
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:(1.8)];
        [image setAlpha:1];
    }else if (a== 1){
        a = 0;
        [image setAlpha:0];
    }
     [UIView commitAnimations;
}

The issue would be that I do not have the potential to run this on my iPad due to the fact that I am receiving multiple memory warnings. The activity-oriented instrument clearly states that "backboardd" uses 287 megabytes when my application has been accessed. My application simply crashes once the heaviest of all view controllers would be accessed. My question would be how I would go about releasing those images until those images are absolutely needed. Keep in mind that many methods are strictly forbidden when using the ARC-oriented method. I appreciate any attempts to assist me by responding to this post.

Update: If you look below, then you will see the segments that give me the ability to load images.

-(IBAction)thisisanaction{
    UIImage *letter = [UIImage imageNamed:@"HelloWorld.png"];
    [imageholder setImage:letter];

Extra Information:

#   Address Category    Timestamp   Live    Size    Responsible Library Responsible Caller
2   0x4c4e000   VM: ImageIO_PNG_Data    00:02.263.286   •   128.59 MB   ImageIO ImageIO_Malloc

Are you using +[UIImage imageNamed:] to load your images? If so, those images will be cached by the system framework until a memory warning comes in. You can try using +[UIImage imageWithContentsOfFile:] to load the images in a way where you can control their lifecycle.

The system frameworks will (theoretically) release images cached via +[UIImage imageNamed:] when a memory warning comes in, but I know from experience that memory warnings are not interlocked -- ie you don't necessarily have enough time to reduce your consumption before your process is killed.

From the code you posted:

-(IBAction)thisisanaction{
    UIImage* letter = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"HelloWorld" ofType: @"png"]];
    [imageholder setImage:letter];

That will keep it from being held quasi-indefinitely in the system cache. Whether the rest of your code properly handles it, I couldn't say, but this will eliminate the side effects of +[UIImage imageNamed:] .

I had a similar memory issue when using +[UIImage imageNamed:] to load lots of different images, but ipmcc's solution of using +[UIImage imageWithContentsOfFile:] instead solved the problem. Thanks alot!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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