iPhone 7 / 7plus上的空snapshotView

时间:2016-09-21 07:01:35

标签: ios iphone swift3 ios10

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

2 个答案:

答案 0 :(得分:7)

使用以下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
        }
    }
}

答案 1 :(得分:0)

适用于:

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