为什么这个 UIImageView 动画会泄漏?

发布于 2024-11-01 07:55:25 字数 966 浏览 3 评论 0原文

使用 Leaks 运行我的应用程序后,我发现以下泄漏。泄漏发生在分配 shimmershimmerAnimation 的位置。我看不出是什么导致了这次泄漏。有人能指出我正确的方向吗?

float duration = .5f;
NSArray *shimmer = [NSArray arrayWithObjects:
                    [UIImage imageNamed:@"shimmer_1.png"],
                    [UIImage imageNamed:@"shimmer_2.png"],
                    [UIImage imageNamed:@"shimmer_3.png"],
                    [UIImage imageNamed:@"shimmer_4.png"],
                    [UIImage imageNamed:@"shimmer_1.png"], nil];

UIImageView *shimmerAnimation = [[UIImageView alloc] initWithFrame:[self bounds]];
[UIView setAnimationDelegate:shimmerAnimation];
[UIView setAnimationDidStopSelector:@selector(removeFromSuperview)];
[shimmerAnimation setAnimationImages:shimmer];
[shimmerAnimation setAnimationDuration:duration];
[shimmerAnimation setAnimationRepeatCount:1];
[shimmerAnimation startAnimating]; 
[self addSubview:shimmerAnimation];
[shimmerAnimation release];

Upon running my application using Leaks, I found the following leaking. The leaks occur where shimmer and shimmerAnimation are allocated. I cannot see what would cause this leak. Can someone point me in the correct direction?

float duration = .5f;
NSArray *shimmer = [NSArray arrayWithObjects:
                    [UIImage imageNamed:@"shimmer_1.png"],
                    [UIImage imageNamed:@"shimmer_2.png"],
                    [UIImage imageNamed:@"shimmer_3.png"],
                    [UIImage imageNamed:@"shimmer_4.png"],
                    [UIImage imageNamed:@"shimmer_1.png"], nil];

UIImageView *shimmerAnimation = [[UIImageView alloc] initWithFrame:[self bounds]];
[UIView setAnimationDelegate:shimmerAnimation];
[UIView setAnimationDidStopSelector:@selector(removeFromSuperview)];
[shimmerAnimation setAnimationImages:shimmer];
[shimmerAnimation setAnimationDuration:duration];
[shimmerAnimation setAnimationRepeatCount:1];
[shimmerAnimation startAnimating]; 
[self addSubview:shimmerAnimation];
[shimmerAnimation release];

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

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

发布评论

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

评论(1

硪扪都還晓 2024-11-08 07:55:25

您将 shimmerAnimation 对象添加为 self 的子视图。这样就可以保留下来了如果 self 泄漏,那么您的 shimmerAnimation 也会泄漏,并且由于它保留了 shimmer,它也会泄漏。所以我会检查 self 看看它在做什么。

对 [UIImage imageNamed:...] 的调用缓存它们加载的图像。但我不认为这些会被视为泄漏。

哦,你正在使用 [UIView setAnimationDelegate:] 但你没有调用 [UIView beginAnimation:] 这意味着 didStopSelector 永远不会被调用,因此,如果你使用它从子视图中删除它(你是)不会。这就是你最有可能的罪魁祸首。

[UIView setAnimationDelegate:] 和朋友用于 UIView 动画,用于 UIImageView 图像动画。

You add the shimmerAnimation object as a subview of self. That'll retain it. If self is leaking, then your shimmerAnimation would be leaking too, and since it's retaining shimmer, it would leak as well. So I'd check self to see what it's doing.

The calls to [UIImage imageNamed:...] cache the images they load. I don't think those come up as leaks though.

Oh, and you're using [UIView setAnimationDelegate:] but you're not calling [UIView beginAnimation:] which means the didStopSelector will never get called, and hence, if you're using that to remove it from the subview (which you are) it won't be. There's your most likely culprit.

[UIView setAnimationDelegate:] and friends are used for UIView animations, not for UIImageView image animations.

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