图像选择器在模拟器上崩溃

时间:2016-10-27 09:34:16

标签: ios swift

我在Xcode 7.3上制作了一个应用程序。今天我安装了XCode8,然后我在代码上改了一些东西,然后当我在模拟器上点击Photo Library Button时,我看到了这个崩溃。在此之后我尝试了iPhone 6并且运行良好。我不知道为什么。我需要开发应用程序,但我不能。只是因为模拟器错误。这太有趣了。

这是崩溃屏幕:

the crash screen

这是我的照片库代码:

@IBAction func PhotoLibraryAction(_ sender: UIButton) {
    let picker = UIImagePickerController()
    picker.delegate = self
    picker.sourceType = .photoLibrary

    present(picker, animated: true, completion: nil)
}

@IBAction func CameraAction(_ sender: UIButton) {
    let picker = UIImagePickerController()
    picker.delegate = self
    picker.sourceType = .camera

    present(picker, animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    if var image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        ImageDisplay.image = image
    }
    dismiss(animated: true, completion: nil)
}

3 个答案:

答案 0 :(得分:7)

尝试将此添加到您的plist:

<key>NSPhotoLibraryUsageDescription</key>
<string>This app requires access to the photo library.</string>
<key>NSCameraUsageDescription</key>
<string>This app requires access to the camera.</string>

答案 1 :(得分:2)

在您的info.plist中,您必须添加此密钥NSPhotoLibraryUsageDescription并将其说明,请查看下图以供参考

enter image description here

答案 2 :(得分:0)

尝试一次

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
NSData *imageData = UIImagePNGRepresentation(chosenImage);