从另一个viewController的视图向另一个viewController的视图添加UIImageView时的内存管理 [英] Memory management when adding a UIImageView to another viewController's view from another viewController's view

查看:97
本文介绍了从另一个viewController的视图向另一个viewController的视图添加UIImageView时的内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在我整个项目中似乎都没有检测到泄漏,并且我有很多执行各种操作的部分.

So it looks like I have no leaks detected throughout my entire project, and I have lots of sections that do various things.

但是,我发现我遇到了一些分配问题,在泄漏仪器中使用标记堆向我证明了这一问题,在大多数情况下,将iPad与外部屏幕配合使用时,这种情况已经发生.将该应用程序编程为在连接到外部屏幕时的行为有所不同.

However, I found that I am having some allocation issues, proven to me with mark heap in leaks instrument, which have happened most of the time when using the iPad with an external screen. The app is programmed to behave differently when connected to an external screen.

我有一个可缩放的UIScrollView地图部分,可以进出.单独使用iPad并检查标记堆时,堆的增长最终将降至0字节,这很好.但是,插入外部屏幕后,图像将发送到使用另一个视图控制器的外部屏幕,并且退出地图时检查标记堆大约每次6-7 MB,而其他堆几乎没有减少增长.不好.

I have a zoomable UIScrollView map section that I can go in and out of. When using the iPad by itself, and checking the mark heap, the heap growth will eventually go down to 0 bytes, which is good. However, with the external screen plugged in, an image is sent to the external screen, which uses another view controller, and checking the mark heap when exiting the map is around 6-7 MB just about every time without much decrease in the other heap growths. BAD.

我发现的事情是,我完全没有正确地处理外部UIImageView的发布,当我浏览它时,这对我来说很明显.我将在后面详细解释.

The thing I found was, that I'm totally not handling the release of the external UIImageView correctly at all, and this was obvious to me when I just glanced over it. I'll explain more about that in a bit.

在我继续解释之前,很重要的一点是,我为此致歉,但是我发誓我听说有一个例外,那就是如果将分配给另一个视图的对象添加到另一个视图中,则不能将它分配给dealloc,因为我不拥有它,因此不负责解除分配.无论是否取消分配,我都会得到相同的行为.但实际上,我认为永远都无法取消分配当前设置代码的方式.所以我认为我的问题是我没有从内存中释放图像视图...但是问题仍然存在,在这种情况下我应该在哪里做!

Important thing before I continue explaining, and I apologize for making this so long, but I swear I've heard there's an exception for not putting an alloc'd object in dealloc if it is added to another view from another view, because I don't own it, thus not responsible for dealloc'ing. Whether I dealloc it or not, I get the same behavior. But I actually don't think it would ever get to dealloc the way my code is setup currently. So I think my problem is that I'm not releasing the image view from memory... but the question remains, where do I do that in this case!

在视图A的initWithFrame方法中,如果已连接,则将视图添加到外部屏幕:

In View A's initWithFrame method, I have the view added to the external screen if it's connected:

    if(exScreenEnabled==1){
        mapImageViewEx = [[UIImageView alloc] init];

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

        UIView *containerExViewP = (UIView*)[del.switchExVC.view viewWithTag:9000];

        [containerExViewP addSubview:mapImageViewEx];

    }

在外部viewController的viewDidLoad方法中,当应用程序启动并且用户选择屏幕分辨率时,添​​加了containerView.与iPad窗口的视图控制器几乎一样,它用于交换视图的内容:

In the external viewController's viewDidLoad method, a containerView had been added when the application started and the user chose the screen resolution. Working almost the same as the iPad window's view controller, it's for swapping in and out views:

CGRect frame = CGRectMake(0, 0, 1024, 768);
containerExView = [[UIView alloc] initWithFrame:frame];
[containerExView setTag:9000];

self.view = containerExView;

让我们将其命名为外部viewController的视图B"

Let's just call this external viewController's "View B"

所以这看起来像是问题开始的地方:当用户在地图中时按下后退按钮时,如果连接了外部屏幕,则视图A会执行视图B侦听的NSNotificationCenter,以删除视图B的子视图.容器.这是View B中的方法:

So this looks like where the problem starts: When the user pushes the back button when they are in the map, if an external screen is connected, View A performs an NSNotificationCenter that View B listens to, to remove subviews in View B's container. Here's the method in View B:

- (void)removeExView{
    [[self.view.subviews objectAtIndex:0] removeFromSuperview];
    NSLog(@"REMOVED EX VIEW");


    UIImage *idleExImage = [UIImage imageNamed:@"idle4.png"];
    UIImageView *idleExImageView = [[UIImageView alloc] initWithImage:(UIImage *)idleExImage];
    CGRect idleExFrame = CGRectMake(0, 0, 1024, 768);
    idleExImageView.frame = idleExFrame;
    [self.view addSubview:idleExImageView];

    [idleExImageView release];
}

从视图B的索引为0的容器中删除该子视图,然后显示默认图像.

The subview's are removed from View B's container at index 0, and then default image is displayed.

因此,我最好的猜测是,即使删除了子视图(即图像视图),也不一定从内存中释放该子视图.但是您将如何访问呢?这甚至是问题吗?也许确实必须对其进行释放,但是由于视图被意外地删除,因此永远无法达到目的.我只是不知道,因为我还不熟悉如何正确处理这种情况.预先感谢您提供的任何帮助.

So, my best guess is, even though the subview was removed, which is the image view, it's not necessarily released from memory. But how would you access this? Is this even the problem? Perhaps it does have to be dealloc'd but can never get to this point because the view is unexpectedly removed first. I just don't know, because I'm not familiar yet how this situation should be handled properly. Thanks in advance for any help you can provide.

推荐答案

[containerExViewP addSubview:mapImageViewEx];

[containerExViewP addSubview:mapImageViewEx];

在此行之后使用[containerExViewP版本];

after this line use [containerExViewP release];

这篇关于从另一个viewController的视图向另一个viewController的视图添加UIImageView时的内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆