在我的iOS应用程序中,我有一个ImageView和两个用于打开相机和图片库的按钮。当我点击其中一个按钮时,应用程序就会关闭。(我在我的设备上运行应用程序,而不是在模拟器上)我必须在我的代码中更改什么?
class PhotoViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var ImageDisplay: UIImageView!
@IBOutlet weak var libraryOutlet: UIButton!
@IBOutlet weak var cameraOutlet: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func openCameraButton(_ sender: UIButton) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .camera
present(picker, animated: true, completion: nil)
}
@IBAction func openLibraryButton(_ sender: UIButton) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .photoLibrary
present(picker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
ImageDisplay.image = info[UIImagePickerControllerOriginalImage] as? UIImage
dismiss(animated: true, completion: nil)
}}
发布于 2016-12-12 21:34:38
在iOS 10中,您需要权限才能访问photoLibrary或camera,方法是将以下键添加到您的plist中,并且您需要使用正确的委托方法。

访问图片库的:
@IBAction func library(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
}访问设备摄像头的:
@IBAction func camera(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}用于拾取和显示图像的:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
ImageDisplay.image = image
}
picker.dismiss(animated: true, completion: nil);
}输出:

发布于 2016-12-12 20:19:12
如果在iOS10上运行,则必须在Info.plist中添加用于访问照相机的条目
将此密钥放入Info.plist
隐私-摄像头使用说明
http://useyourloaf.com/blog/privacy-settings-in-ios-10/
如果不是,应用程序将会崩溃,就像您的情况一样
发布于 2016-12-12 20:34:01
如果你在iOS10中开发应用程序,那么必须在你的info.plist中添加隐私权限设置,并在需要隐私的地方描述一些东西。
隐私设置列表:
蓝牙共享- NSBluetoothPeripheralUsageDescription
日历- NSCalendarsUsageDescription
CallKit - NSVoIPUsageDescription
摄像头- NSCameraUsageDescription
联系人- NSContactsUsageDescription
健康- NSHealthShareUsageDescription & NSHealthUpdateUsageDescription
HomeKit - NSHomeKitUsageDescription
位置- NSLocationUsageDescription,NSLocationAlwaysUsageDescription,
NSLocationWhenInUseUsageDescription
媒体库- NSAppleMusicUsageDescription
麦克风- NSMicrophoneUsageDescription
Motion - NSMotionUsageDescription
图片- NSPhotoLibraryUsageDescription
提醒- NSRemindersUsageDescription
语音识别- NSSpeechRecognitionUsageDescription
SiriKit - NSSiriUsageDescription
电视提供商- NSVideoSubscriberAccountUsageDescription
https://stackoverflow.com/questions/41100717
复制相似问题