【问题标题】:Swift 3 / Why does the snapshot from snapshotView appears blank in iPhone 7 Plus Simulator?Swift 3 / 为什么快照视图中的快照在 iPhone 7 Plus 模拟器中显示为空白?
【发布时间】:2016-10-22 03:43:54
【问题描述】:

我正在使用以下代码来创建自定义菜单转换。我花了两个小时试图弄清楚为什么快照显示为空白(仅使用 iPhone 7 Plus 模拟器)。但是当我想为这个线程创建一个视频并将其制作成 gif 时,它在我的 iPhone 6S Plus 上运行

更新:也适用于 iPhone 6S 模拟器。但在 7 Plus 中仍然没有。

import UIKit

class PresentMenuAnimator : NSObject {
}

extension PresentMenuAnimator : UIViewControllerAnimatedTransitioning {
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.6
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

        let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)
        let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
        let containerView = transitionContext.containerView
        containerView.insertSubview((toVC?.view)!, belowSubview: (fromVC?.view)!)

        // replace main view with snapshot
        let snapshot = fromVC?.view.snapshotView(afterScreenUpdates: false)
        snapshot?.tag = MenuHelper.snapshotNumber
        snapshot?.isUserInteractionEnabled = false
        snapshot?.layer.shadowOpacity = 0.7
        containerView.insertSubview(snapshot!, aboveSubview: (toVC?.view)!)
        fromVC?.view.isHidden = true

        UIView.animate(
            withDuration: transitionDuration(using: transitionContext),
            animations: {
                snapshot!.center.x += UIScreen.main.bounds.width * MenuHelper.menuWidth
            },
            completion: { _ in
                fromVC?.view.isHidden = false
                transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
            }
        )
    }
}

iPhone 6S Plus(物理设备)iOS 10

iPhone 7 Plus(模拟器)iOS 10

为什么模拟器上的快照是空白的?

GitHub test project

【问题讨论】:

  • 我已尝试运行该项目,它在 6s 模拟器 (14A345) 上运行良好
  • iPhone 7 Plus sim? 14A345
  • 也适用于 6s 模拟器。刚试过。但 7 加,不是
  • This thread 建议这只发生在模拟器中,而不是在真正的 iPhone 7 设备上。
  • 对这个案子也很好奇。找到原因了吗?

标签: ios swift


【解决方案1】:

替代解决方案,

添加这个UIView 扩展,

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
        }
    }
}

更新您的以下代码,

let snapshot = fromVC?.view.snapshotView(afterScreenUpdates: false)

与,

let snapshot = fromVC?.view.snapshotView()

Ref

【讨论】:

  • 这并不能完全回答为什么 snapshotview(afterScreeUpdates:) 不起作用的问题,只是提供了一种替代解决方案。您知道为什么该方法不起作用吗?我遇到了同样的问题
  • 是的..也是同样的问题......而且绘制成图像很慢......所以我也想知道问题是什么......
  • 谢谢@Zaid,但你知道为什么它与 snapshotView(afterScreenUpdates:) 如此不同吗???很高兴了解更多信息
  • 是否有任何关于为什么会发生这种情况的更新?我找不到任何关于为什么此方法可能返回 nil
  • 对不起,我不知道原始问题的原因,这只是上面写的替代解决方案。
猜你喜欢
  • 2017-01-29
  • 2013-09-24
  • 1970-01-01
  • 2014-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多