• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 为什么 UILabel 会增加内存使用量?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 10:04:46 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

所以,我有一些边界(addBoundaryWithIdentifier:),一个生成 block 的代码(UIView方 block )每秒使用 performSelector:,方 block 会掉落,如果与边界发生碰撞,我会移除项目。

现在,如果我将 UILabel 添加到 View 中,内存使用量会慢慢增加到 15Mb,然后停止攀升。

不使用 UILabel 的内存: image 与 UILabel 一起使用的内存: image 几分钟后使用 UILabel 的内存: image

这里发生了什么?如何解决?我应该解决这个问题吗?

这是我正在使用的代码。

@implementation ESViewController {
    UIPushBehavior *pushBlock;
    UIDynamicAnimator *mainAnimator;
    UICollisionBehavior *blockCollision;
}

- (void)viewDidLoad {
    [super viewDidLoad];

//    UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(110, 400, 100, 50)];
//    [label setText"Hello"];
//    [label setTextAlignment: NSTextAlignmentCenter];
//    [self.view addSubview: label];

    mainAnimator = [[UIDynamicAnimator alloc] initWithReferenceView: self.view];

    blockCollision = [[UICollisionBehavior alloc] initWithItems: @[]];
    [blockCollision addBoundaryWithIdentifier"bottom"
                                    fromPoint:CGPointMake(0, 480)
                                      toPoint:CGPointMake(320, 480)];
    [blockCollision addBoundaryWithIdentifier"left"
                                    fromPoint:CGPointMake(0, 480)
                                      toPoint:CGPointMake(0, 0)];
    [blockCollision addBoundaryWithIdentifier"right"
                                    fromPoint:CGPointMake(320, 480)
                                      toPoint:CGPointMake(320, 0)];
    [blockCollision addBoundaryWithIdentifier"top"
                                    fromPoint:CGPointMake(0, -40)
                                      toPoint:CGPointMake(320, -40)];
    [blockCollision setCollisionDelegate: (id<UICollisionBehaviorDelegate>)self];
    [mainAnimator addBehavior: blockCollision];

    pushBlock = [[UIPushBehavior alloc] initWithItems: @[] mode: UIPushBehaviorModeContinuous];
    [pushBlock setAngle: M_PI/2 magnitude:0.05];
    [mainAnimator addBehavior: pushBlock];

    [self movingBlocks];
}

- (void) movingBlocks {
    UIView *block = [[UIView alloc] initWithFrame:CGRectMake(arc4random() % 320, -20, 20, 20)];
    [block setBackgroundColor: [UIColor brownColor]];
    [self.view addSubview: block];
    [blockCollision addItem: block];
    [pushBlock addItem: block];
    [self performSelectorselector(movingBlocks) withObject:nil afterDelay:0.05];
}

- (void)collisionBehaviorUICollisionBehavior *)behavior
      beganContactForItemid<UIDynamicItem>)item
   withBoundaryIdentifierid<NSCopying>)identifier
                  atPointCGPoint)p {
    [pushBlock removeItem:item];
    [blockCollision removeItem:item];
    [(UIView*)item removeFromSuperview];
}

@end



Best Answer-推荐答案


我已经重现了您所描述的症状,但我认为 Xcode 在对您撒谎,您应该打开雷达 (bugreport.apple.com)。

如果您在 Instruments 中运行它,内存使用情况将完全符合您的预期。我已经尝试在 Release模式下使用 Xcode 配置文件(就像 Instruments 一样),并且我已经在所有 iPad 模拟器上运行。该症状在 Xcode 中非常可重现,而 Instruments 显示了预期的结果并且非常稳定。我什至将一个在 Xcode 中显示增长的正在运行的进程转移到 Instruments 中,但 Instruments 仍然没有增长。我会说 Xcode 在这里被破坏了。

关于ios - 为什么 UILabel 会增加内存使用量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23809048/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap