【问题标题】:Correctly releasing returned UIImage正确释放返回的 UIImage
【发布时间】:2013-05-09 11:22:06
【问题描述】:

我调用以下方法来获取我在 UIImageView 中显示的图像。完成 UIImageView 后,我会发布 myImageView.image。我对自己的操作方式很满意,但 Instruments 显示 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;
}

提前致谢

【问题讨论】:

  • 你可以使用自动释放

标签: ios xcode memory-leaks uiimageview instruments


【解决方案1】:

在您退回图片时尝试使用自动释放。

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

【讨论】:

    猜你喜欢
    • 2011-02-25
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 2011-05-07
    • 2011-09-09
    • 1970-01-01
    相关资源
    最近更新 更多