我的第一个问题是:)最近我将Xcode更新为8,并且resizableSnapshotView方法在某些模拟器上无法正常工作。 snapshotView在所有装有iOS9/10和iPhone6s下的模拟器的测试设备上都可以很好地工作,但在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创建快照。

我可以确认这可以在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
        }
    }
}

10-06 13:04