CGImageCreate:无效的图像alphaInfo:kCGImageAlphaNone应该是kCGImageAlphaNoneSkipLast

问题描述

我正在尝试将UIVIew捕获为图像,然后可以对其进行操作。我有可在Xcode 11.7 / simulator(运行iOS 13.7)中运行的代码,但抛出此错误:

CGImageCreate:无效的图像alphaInfo:kCGImageAlphaNone。应该是kCGImageAlphaNoneSkipLast

在实际设备上运行会导致崩溃。

有什么主意,我该如何在不使用Alpha的情况下创建此图像,或者在事后将其删除?

extension UIView  {
    // render the view within the view's bounds,then capture it as image
  func asImage() -> UIImage {
    let renderer = UIGraphicsImageRenderer(bounds: bounds)
    return renderer.image(actions: { rendererContext in
        layer.render(in: rendererContext.cgContext)
    })
  }
}

通过以下代码调用它:

@IBAction func reactionListButtonTapped(_ sender: UIButton) {
  guard let reactionVC = storyboard?.instantiateViewController(withIdentifier: "ReactionViewController")
    as? ReactionViewController else {
      
    assertionFailure("No view controller ID ReactionViewController in storyboard")
    return
  }

  // take a snapshot of current view and set it as backingImage
  reactionVC.backingImage = self.view.asImage()

  self.present(reactionVC,animated: false,completion: nil)
}

感谢您的帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...