【问题标题】:An empty snapshotView on iPhone 7/7plusiPhone 7/7plus 上的空快照视图
【发布时间】:2017-01-29 06:56:51
【问题描述】:

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

【问题讨论】:

  • 我也有同样的经历,很高兴听到它只发生在模拟器上。
  • 完全相同的问题,但是我还没有在真实设备上测试过这个
  • 致阿尔夫。不幸的是,我没有真实的设备,我不确定这是否发生在真实的设备上。
  • 在 Xcode 8.1/iOS 10.1 Beta 1 中仍然存在问题。
  • 无法在装有 iOS 10.0 的 iPhone 7 设备上重现它,所以仍然希望这是一个仅限模拟器的问题。 @LYM 你向 Apple 提交了雷达吗?

标签: ios iphone swift3 ios10


【解决方案1】:

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

【讨论】:

  • 谢谢,这行得通。但是,我的主要事情是使用resizableSnapshotView这个功能。我尝试了一些时间用您的代码重写它,但失败了。你能告诉我如何用框架剪辑快照视图吗?
  • 这不起作用。它对我产生的结果与旧方法相同。用 Xcode 8 和 iPhone 7/7+ 模拟器试了一下。
  • 有人知道为什么会这样吗?
  • 是的。我更新到 Xcode 8.1 后它也不起作用。
【解决方案2】:

适用于:

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

斯威夫特 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()
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-22
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多