iPhone 7 / 7plus上的一个空的snapshotView

我的第一个问题在这里:)最近我更新我的Xcode为8, resizableSnapshotView方法不能在一些模拟器上正常工作。 snapshotView在iOS9 / 10下的所有testing设备和iPhone6下的模拟器上都能正常工作,但在iPhone7 / 7p模拟器上却是空的。 我想7/7p可能需要一些权限来访问快照,但我不知道它们是什么。

 let cell = self.tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! CalendarCell var topFrame = cell.frame topFrame.origin.y = tableView.contentOffset.y topFrame.size.height -= tableView.contentOffset.y topSnapshotView = tableView.resizableSnapshotView(from: topFrame, afterScreenUpdates: false, withCapInsets: UIEdgeInsets.zero) 

使用下面的UIView扩展来使用CoreGraphics创build一个快照。

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

 public extension UIView { public func snapshotImage() -> UIImage? { UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, 0) drawHierarchy(in: bounds, afterScreenUpdates: false) let snapshotImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return snapshotImage } public func snapshotView() -> UIView? { if let snapshotImage = snapshotImage() { return UIImageView(image: snapshotImage) } else { return nil } } } 

适用于:

  • 版本8.1(8B62)
  • 模拟器版本10.0(SimulatorApp-700.14

Swift 3

 import Foundation import UIKit extension UIView { func snapshotView() -> UIView? { guard let image = snapshotImage() else { return nil } return UIImageView(image: image) } func snapshotImage() -> UIImage? { UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, contentScaleFactor) defer { UIGraphicsEndImageContext() } drawHierarchy(in: bounds, afterScreenUpdates: false) return UIGraphicsGetImageFromCurrentImageContext() } }