为什么这个 UIImageView 动画会泄漏?
使用 Leaks 运行我的应用程序后,我发现以下泄漏。泄漏发生在分配 shimmer
和 shimmerAnimation
的位置。我看不出是什么导致了这次泄漏。有人能指出我正确的方向吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将 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.