使用后释放UIImage

时间:2010-09-22 11:24:37

标签: uiimageview uiimage release memory-leaks

我想学习分配uiimage并将其释放用于内存管理的正确方法 当我使用以下代码时,它会在解除分配时崩溃,
   如果我不使用[成像器发布];
程序没有崩溃,但它显示成像器的泄漏。任何解决方案的想法?

UIImage *imager = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:s@"nameofimage" ofType:@"jpg"]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:imager];

[imageView release];

[imager release];

1 个答案:

答案 0 :(得分:0)

正确的做内存管理方式就是你所做的。

UIImage *imager = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"nameofimage" ofType:@"jpg"]]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:imager];
[imager release];
[imageView release]; 

如果您首次发布uiimage而不是uiimageview

,可能会有效

我在这段代码中没有发现任何错误。如果应用程序崩溃,请检查在释放后是否在其他任何位置使用相同的对象引用。