Ios 内存泄漏问题

Ios 内存泄漏问题,ios,memory-management,uiimageview,alloc,Ios,Memory Management,Uiimageview,Alloc,我有很多内存泄漏。。。 例如,我有一个UIImageView,每次更新时图像都会翻转(动画大约为30fps,因此此图像会更新并翻转很多) 它有大量内存泄漏,因此我在翻转一次后将其释放: image2 = [[UIImage alloc] initWithCGImage:image2.CGImage scale:image2.scale orientation:UIImageOrientationUpMirrored]; [image2 release]; 但问题不在于,如果我尝试再次运行该代码

我有很多内存泄漏。。。 例如,我有一个UIImageView,每次更新时图像都会翻转(动画大约为30fps,因此此图像会更新并翻转很多)

它有大量内存泄漏,因此我在翻转一次后将其释放:

image2 = [[UIImage alloc] initWithCGImage:image2.CGImage scale:image2.scale orientation:UIImageOrientationUpMirrored];
[image2 release];
但问题不在于,如果我尝试再次运行该代码,应用程序就会冻结(我猜你不能发布一些东西,然后再次使用它?(这对整个内存分配和发布内容来说是一种新的方式


我该怎么办?如果图像已被释放,我是否在尝试翻转它之前重新定义它?谢谢!

您需要将图像视图的
image
属性设置为结果图像,然后释放分配的图像。例如:

image2 = [[UIImage alloc] initWithCGImage:image2.CGImage scale:image2.scale orientation:UIImageOrientationUpMirrored];
self.someImageView.image = image2;
[image2 release];
或者,您可以自动释放返回的图像。如下所示:

image2 = [[[UIImage alloc] initWithCGImage:image2.CGImage scale:image2.scale orientation:UIImageOrientationUpMirrored] autorelease];
self.someImageView.image = image2;

编辑:在你澄清了你的问题之后,这里有一个更好的方法来垂直翻转你的图像

//lets suppose your image is already set on the image view
imageView.transform = CGAffineTransformIdentity;
imageView.transform = CGAffineTransformMakeScale(1.0, -1.0);
然后,当您要将其更改回正常状态时:

imageView.transform = CGAffineTransformIdentity;

您需要将图像视图的
image
属性设置为结果图像,然后释放分配的图像。例如:

image2 = [[UIImage alloc] initWithCGImage:image2.CGImage scale:image2.scale orientation:UIImageOrientationUpMirrored];
self.someImageView.image = image2;
[image2 release];
或者,您可以自动释放返回的图像。如下所示:

image2 = [[[UIImage alloc] initWithCGImage:image2.CGImage scale:image2.scale orientation:UIImageOrientationUpMirrored] autorelease];
self.someImageView.image = image2;

编辑:在你澄清了你的问题之后,这里有一个更好的方法来垂直翻转你的图像

//lets suppose your image is already set on the image view
imageView.transform = CGAffineTransformIdentity;
imageView.transform = CGAffineTransformMakeScale(1.0, -1.0);
然后,当您要将其更改回正常状态时:

imageView.transform = CGAffineTransformIdentity;

最简单的方法可能是将image2设置为保留属性,然后将其分配给
self.image2
,而不是普通的
image2
。这将导致在分配新值时释放旧图像。但随后需要向
[UIImage alloc]添加一个
自动释放
调用init…
调用释放alloc执行的保留操作可能最简单的方法是将image2设置为保留属性,然后将其分配给
self.image2
,而不是普通的
image2
。这将导致在分配新值时释放旧图像。但随后需要添加
autorelease
调用您的
[UIImage alloc]init…
调用以释放
alloc

所做的保留。重复使用相同的变量名会使它变得不必要的混乱。添加一个临时变量

UIImage* image; // assuming you set this up earlier, and that it's retained
UIImage* flippedImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationUpMirrored];
// Now we're done with the old image. Release it, so it doesn't leak.
[image release];
// And set the variable "image" to be the new, flipped image:
image = flippedImage;

重复使用相同的变量名会使它变得不必要的混乱。请添加一个临时变量

UIImage* image; // assuming you set this up earlier, and that it's retained
UIImage* flippedImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationUpMirrored];
// Now we're done with the old image. Release it, so it doesn't leak.
[image release];
// And set the variable "image" to be the new, flipped image:
image = flippedImage;


你怎么知道有泄漏?你运行过Analyzer吗?(有人假设这是一个非ARC应用程序)。这是一种猜测,没有看到你在使用
image2
之后做了什么,但试着做
[image2 autorelease]
而不是
release
。或者只做
[image2 release]
使用完毕后。我使用仪器在Xcode中运行配置文件并发现漏洞。我每1/30秒运行一次此代码,因为图像每1/30秒更新一个新镜像图像,我需要再次翻转它。因此,如果我释放它,请更新图像并运行此代码以尝试再次翻转它…它具有alr已经发布,应用程序冻结/:使其像
UIImage*image2=[[UIImage alloc]initWithCGImage:image2.CGImage缩放:image2.scale方向:UIImageOrientationUpMirrored]
您的冻结问题将得到解决,我想说的是在您发布的范围内定义并分配它。您如何知道有泄漏?您是否运行Analyzer?(有人假设这是一个非ARC应用程序。)这是一种疯狂的猜测,没有看到您在使用
image2
之后正在做什么,但是尝试执行
[image2 autorelease]
而不是
发布
。或者只执行
[image2 release]
使用完毕后。我使用仪器在Xcode中运行配置文件并发现漏洞。我每1/30秒运行一次此代码,因为图像每1/30秒更新一个新镜像图像,我需要再次翻转它。因此,如果我释放它,请更新图像并运行此代码以尝试再次翻转它…它具有alr已经发布,应用程序冻结/:使其像
UIImage*image2=[[UIImage alloc]initWithCGImage:image2.CGImage缩放:image2.scale方向:UIImageOrientationUpMirrored]
你的冻结问题到时候就会解决,我想说的是在你发布它的范围内定义它并分配它。好吧,但是如果我需要在1/30秒后再做一次,而图像已经发布了,我怎么能再做一次呢?我不太确定你在做什么。实际上,你的代码毫无意义。为什么是you将
image2
设置为同一图像(
image2
)它已经是这样了?你是镜像图像还是什么的?可能不是最优化的方式。你有没有想过改用变换?我把它设置为自身,但镜像了…现在当前的图像2是颠倒的,所以我要重新定位它。但我必须使用代码,每次图像是u更新为新图像(每30分之一秒更新一次)因此,如果我发布它,然后更新图像,然后尝试重新定向,应用程序将冻结并退出。好吧,你可以始终使用下面指出的保留属性,如
Hot Licks
。但在你的情况下,我会尝试转换图像,而不是每秒重新分配30次。这是一个主要的慢动作wn.好的,但是如果我需要在1/30秒后再次执行此操作,并且图像已经发布,我如何才能再次执行此操作?我不太确定您正在执行的操作。实际上,您的代码毫无意义。为什么要将
image2
设置为同一图像(
image2
)它已经是这样了?你是镜像图像还是什么的?可能不是最优化的方式。你有没有想过改用变换?我正在将它设置为自身,但镜像了…现在当前的图像2是颠倒的,所以我正在重新定位它。但我必须使用t