UIImage setImage:导致内存泄漏

时间:2012-04-02 13:01:49

标签: ios objective-c memory-leaks uiimageview uiimage

我从一个非常简单的函数中得到了内存泄漏问题。这是我的代码:

  -(void) drawPuzzle
 {

      gameView.image=nil;     

      UIGraphicsBeginImageContext(gameView.frame.size);


      [gameView.image  drawInRect:CGRectMake(0,
                                             0, 
                                             gameView.frame.size.width,
                                             gameView.frame.size.height)];



       CGContextRef con=UIGraphicsGetCurrentContext();
       CGContextMoveToPoint(con, 20,0);
       CGContextAddLineToPoint(con,20, gameView.frame.size.height);
       CGContextStrokePath(con);


       UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
       UIGraphicsEndImageContext();
       [gameView setImage:newImage];


  }

文书报告如下:

Event Type    RefCt      Responsible Caller

Malloc        1          +[UIImage imageWithCGImage: scale: orientation:] 
Autorelease              +[UIImage imageWithCGImage: scale: orientation:] 
Retain        2          -[UIImageView setImage:]
Release       1          -[NSAutoreleasePool drain]       

1 个答案:

答案 0 :(得分:-2)

setImage:方法保留了您的newImage。为避免内存泄漏,您需要在newImage

之后发布setImage
   UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
   UIGraphicsEndImageContext();
   [gameView setImage:newImage];
   [newImage release];