iPhone 7/7plus 上的空快照视图

An empty snapshotView on iPhone 7/7plus

我的第一个问题:) 最近我将我的 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
        }
    }
}

工作于:

  • 版本 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()
    }
}