将 UIImageView 添加到另一个窗口视图时如何管理内存?

发布于 2024-10-20 16:43:02 字数 1076 浏览 4 评论 0原文

因为我认为我最近的另一篇文章吓跑了所有人(

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

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

现在我有:

            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 中释放图像不会执行任何操作。

Since I think I scared everyone away with my other recent post ( Memory management when adding a UIImageView to another viewController's view from another viewController's view ) and I'm running out of time, I'd just like to ask this in simpler terms. If you want to know I'm doing with more detail, refer to the other post.

How do you manage memory, and releasing, when adding a UIImageView to another windows view?

i.e. I'm creating a UIImageView in one view, and adding it as a subview in another windows view. The other window is being used by a TV.

Right now I have:

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

Problem I'm having is that it keeps stacking up memory use every time I exit the view. Releasing the image in the iPad screen view's dealloc does nothing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┼── 2024-10-27 16:43:02

更改

        mapImageViewEx = [[UIImageView alloc] init];

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

自动释放意味着引用计数将在将来的某个未知时刻减少。

addSubview 会增加计数,所以你不必担心它。

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

change

        mapImageViewEx = [[UIImageView alloc] init];

to

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

autorelease means that the reference count will be decremented at some unknown point in the future.

the addSubview will increment the count, so you don't have to worry about it.

As long as the parent view is properly disposed of, mapImageViewEx will be released.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文