ios – iPhone 7 / 7plus上的空snapshotView

前端之家收集整理的这篇文章主要介绍了ios – iPhone 7 / 7plus上的空snapshotView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的第一个问题:)最近我更新我的 Xcode到8,resizableSnapshotView方法在一些模拟器上不能正常工作. snapshotView在所有使用iOS9 / 10的测试设备和iPhone6s下的模拟器上运行良好,但在iPhone7 / 7p模拟器上为空.我认为7 / 7p可能需要一些权限来访问快照,但我不知道它们是什么.
  1. let cell = self.tableView.cellForRow(at: IndexPath(row: 0,section: 0)) as! CalendarCell
  2. var topFrame = cell.frame
  3. topFrame.origin.y = tableView.contentOffset.y
  4. topFrame.size.height -= tableView.contentOffset.y
  5. topSnapshotView = tableView.resizableSnapshotView(from: topFrame,afterScreenUpdates: false,withCapInsets: UIEdgeInsets.zero)

解决方法

使用以下UIView扩展使用CoreGraphics创建快照.

我可以确认这在iPhone 7模拟器上的作品.

  1. public extension UIView {
  2.  
  3. public func snapshotImage() -> UIImage? {
  4. UIGraphicsBeginImageContextWithOptions(bounds.size,isOpaque,0)
  5. drawHierarchy(in: bounds,afterScreenUpdates: false)
  6. let snapshotImage = UIGraphicsGetImageFromCurrentImageContext()
  7. UIGraphicsEndImageContext()
  8. return snapshotImage
  9. }
  10.  
  11. public func snapshotView() -> UIView? {
  12. if let snapshotImage = snapshotImage() {
  13. return UIImageView(image: snapshotImage)
  14. } else {
  15. return nil
  16. }
  17. }
  18. }

猜你在找的iOS相关文章