gpt4 book ai didi

ios - UIImage setImage:导致内存泄漏

转载 作者:行者123 更新时间:2023-12-01 17:12:22 26 4
gpt4 key购买 nike

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

  -(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]

最佳答案

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

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

关于ios - UIImage setImage:导致内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9976979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com