ios - 拍摄 View 快照时出现 fatal error : unexpectedly found nil while unwrapping an Optional value

标签 ios swift uipageviewcontroller snapshot

当我拍摄当前 View 的快照时,我的应用程序崩溃了,该 View 是 UIViewController 显示由 UIPageViewController 生成的照片。

  override func viewDidLoad() {        
    super.viewDidLoad()
    Utils.getImageAsync(photo!.url!, completion: { (photoView) -> () in            
        //Create Basic View Components
        self.view.addSubview(self.createHeaderView())
        self.view.addSubview(self.createScrollView())            
        //Create Blurred View
        self.blurredBgImage!.image = self.blurWithGPUImage(self.takeSnapshotOfView(self.view)) // fatal error: unexpectedly found nil while unwrapping an Optional value
    })

 }

为什么会这样?

最佳答案

通过使用“!”您正在强制展开一个 nil 可选。

您可以使用强制展开“!”如果您知道可选项没有 nil 值。如果您强制解包一个包含 nil 值的可选值,它将导致您遇到的崩溃。

那个?展开可以防止这种情况。基本上它检查“if X = nil”。如果是这样,请忽略/什么也不做。防止崩溃。

我希望我的胡言乱语是有道理的。它确实在我的脑海中:)

关于ios - 拍摄 View 快照时出现 fatal error : unexpectedly found nil while unwrapping an Optional value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31265955/

相关文章:

ios - 大型 JSON 文件 Swift

ios - Swift 3.0 标签空白(零值?)

ios - 在 IOS 6 上运行的应用程序在 IOS 5 中崩溃

swift - replacingOccurrences() 中的错误?

ios - 如何去除UIPageViewController的底部空隙

swift - PageViewController 嵌入在其他 PageViewController 中

ios - UIPageViewController滑动时黑色背景多

android - 为什么应用程序使用推送通知而不是 VPN(虚拟专用网络)?

ios - 遍历 NSDictionary 并将其保存到对象类型的数组中

iphone - 如何在Three20的AppDelegate中添加全局右栏按钮(UINavigationController)