OStack程序员社区-中国程序员成长平台

标题: ios - CALayer 的图像未显示 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 03:15
标题: ios - CALayer 的图像未显示

我正在尝试使用 CALayer 显示图像,但图像未显示。我已经验证了 'image' 不是 nil,image.size 是正确的,并且 layer.contents 是一个有效的 CABackingStore。

- (void)viewDidLoad
{
  [super viewDidLoad];

  layer = [[CALayer alloc] init];
  [self.view.layer addSublayer:layer];
  UIImage *image = [UIImage imageNamed"download.jpeg"];
  layer.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
  layer.position = self.view.center;
  layer.contents = (id)image.CGImage;

  [layer setNeedsDisplay];
}

这可能是什么问题?



Best Answer-推荐答案


删除 [layer setNeedsDisplay]; 应该会纠正这种行为。

来自 docs (强调):

Calling this method causes the layer to recache its content. This results in the layer potentially calling either the displayLayer: or drawLayer:inContext: method of its delegate. The existing content in the layer’s contents property is removed to make way for the new content.

关于ios - CALayer 的图像未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17930454/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4