Swift 2:UIImagePickerController返回nil或不是UIImage的对象

时间:2017-04-22 00:43:17

标签: ios swift swift2 uiimage uiimagepickercontroller

我刚收到一条imagePickerController:picker: didFinishPickingMediaWithInfo的孤立崩溃报告,其中包含EXC_BREAKPOINT消息(我理解通常在强制解包nil值时发生)。崩溃的代码是:

public func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    let pickedImage = info[UIImagePickerControllerOriginalImage] as! UIImage

    dismissViewControllerAnimated(true, completion: nil)
    picker.delegate = nil

    // ...do something with pickedImage...
}

我假设我需要检查是否已经像这样返回了UIImage:

public func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    dismissViewControllerAnimated(true, completion: nil)
    picker.delegate = nil

    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        // ...do something with pickedImage...
    }
    else {
        // Show unsupported media type error dialog
    }
}

我将选取器媒体类型设置为图像:

picker.mediaTypes = [kUTTypeImage as String]

所以我很惊讶didFinishPickingMediaWithInfo正在返回UIImage以外的东西。任何想法为什么或可能会返回什么?

0 个答案:

没有答案