正确释放返回的UIImage

时间:2013-05-09 11:22:07

标签: ios xcode memory-leaks uiimageview instruments

我调用以下方法来获取我在UIImageView中显示的图像。当我完成UIImageView时,我发布了myImageView.image。我很高兴我这样做但仪器显示UIImage泄漏。我应该如何发布下面的UIImage实例?

-(UIImage *)getImageWithRef:(NSString *)ref {
    UIImage *myImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"foobar_%@",ref]];
    if (myImage == nil) {
        myImage = [[UIImage alloc] initWithContentsOfFile:@"foobar_defaultImage"];
    }
    return myImage;
}

提前致谢

1 个答案:

答案 0 :(得分:2)

返回图片时尝试使用自动释放。

-(UIImage *)getImageWithRef:(NSString *)ref {
    UIImage *myImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"foobar_%@",ref]];
    if (myImage == nil) {
        myImage = [[UIImage alloc] initWithContentsOfFile:@"foobar_defaultImage"];
    }
    return [myImage autorelease];
}