【问题标题】:How do you manage memory when adding a UIImageView to another windows view?将 UIImageView 添加到另一个 Windows 视图时如何管理内存?
【发布时间】:2011-07-11 11:12:47
【问题描述】:

由于我认为我最近的另一篇帖子 (Memory management when adding a UIImageView to another viewController's view from another viewController's view) 吓跑了所有人,而且我的时间不多了,所以我想用更简单的方式问这个问题。如果您想知道我正在做的更多细节,请参阅另一篇文章。

在将 UIImageView 添加到另一个 windows 视图时,如何管理内存和释放?

即我正在一个视图中创建 UIImageView,并将其作为子视图添加到另一个 Windows 视图中。电视正在使用另一个窗口。

现在我有:

            mapImageViewEx = [[UIImageView alloc] init];

            // I think you can ignore these two things below
            CGPoint p = mapScrollView.contentOffset;
            mapImageViewEx.frame = CGRectMake((p.x*-1), (p.y*-1), mapImageView.frame.size.width, mapImageView.frame.size.height);

            NSString *mapExFileLocation = [[NSBundle mainBundle] pathForResource:[map_List objectAtIndex:mapNum] ofType:@"png"];
            NSData *mapExIMGData = [NSData dataWithContentsOfFile:mapExFileLocation];
            mapImageViewEx.image = [UIImage imageWithData:mapExIMGData];

            // finds the other window's view controller's view
            UIView *containerExViewP = (UIView*)[del.switchExVC.view viewWithTag:9000];

            // adds it
            [containerExViewP addSubview:mapImageViewEx];

我遇到的问题是,每次我退出视图时,它都会不断累积内存使用量。在 iPad 屏幕视图的 dealloc 中释放图像没有任何作用。

【问题讨论】:

    标签: iphone ipad memory uiimageview screen


    【解决方案1】:

    改变

            mapImageViewEx = [[UIImageView alloc] init];
    

            mapImageViewEx = [[[UIImageView alloc] init] autorelease];
    

    autorelease 意味着引用计数将在未来某个未知点递减。

    addSubview 将增加计数,因此您不必担心。

    只要正确处理父视图,mapImageViewEx就会被释放。

    【讨论】:

    • 试过了。它在我第二次进入图片进入电视的视图时崩溃。有什么想法吗?
    • 那你可能有不同的问题。您发布的代码中没有任何其他明显的内存泄漏。
    • Leaks 应用未检测到泄漏。
    • 对不起。我应该说内存错误。你有相反的问题。如果它崩溃,则可能是某些东西被过度释放了。你在其他地方发布 mapImageViewEx 吗?
    • 根据这篇文章stackoverflow.com/questions/2507145/… imageWithData 返回一个自动释放的对象。提供的答案显示 NSData 和 UIImage 正在发布
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多