ios - requestImageForAsset 始终返回 nil - 即使在检查 nil 后也是如此

标签 ios swift swift2

所以,我遇到了一个奇怪(且令人沮丧)的问题。 我正在循环访问图像选择器返回的一组资源:

let manager = PHImageManager.defaultManager()
let options = PHImageRequestOptions()
for asset in assets {
    var image = UIImageView?
    manager.requestImageForAsset(asset, targetSize: CGSize(width: asset.pixelWidth, height: asset.pixelHeight), contentMode: .AspectFit, options: option, resultHandler: { (result, info) -> Void in
        if result == nil {
            print("Nil alert!")
            return
        }
        print("not nil")
        image!.image = result

    }
}

在控制台中我收到了预期的消息:

not nil

紧接着这条消息:

fatal error: unexpectedly found nil while unwrapping an Optional value

我已经搞了好几个小时了,但我似乎无法弄清楚问题到底是什么。如果我尝试打印 result它直接说类似 <UIImage: 0x12f780a20>, {45, 60}

欢迎任何帮助:)

最佳答案

定义 image 后,您没有对其进行任何设置,因此此处的值将为 nil:

 var image = UIImageView?

当您在此处使用 ! 强制解开它时,您将遇到运行时错误:

 image!.image = result

关于ios - requestImageForAsset 始终返回 nil - 即使在检查 nil 后也是如此,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37485302/

相关文章:

iOS/信标 : Reset didEnterRegion possible?

ios - MFMailComposeViewController 不关闭

ios - 双击 UITableview 中的单元格以执行 segue

ios - 在图层之上绘制#rect 核心图形?

ios - UILabel作为tableViewCell的accessoryView向上移动

ios - 具有强引用的 View Controller 导致内存泄漏

ios - UIButton 在工作前轻按 3 次? swift

swift - Swift 3 中的 Alamofire 与 iOS 8

ios - 选择具有 ID 的单元格时执行 segue

ios - iOS 9/Swift 2.0 中的 UIDynamics 问题