访问相机和PhotoLibrary

在我的iOS应用程序中,我有一个ImageView和两个按钮,用于打开相机和photolibrary。 当我点击其中一个按钮时,应用程序关闭。 (我在我的设备上运行应用程序,而不是模拟器)我的代码中需要更改什么?

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) } 

}

在iOS 10中,您需要通过向plist添加以下键来访问photoLibrary或相机,并且您需要使用正确的委托方法。

在此处输入图像描述

访问照片库:

 @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); } 

输出:

在此处输入图像描述

如果您在iOS10上运行,则必须在Info.plist中添加用于访问摄像头的条目

把这个键放在Info.plist中

隐私 – 相机使用说明

http://useyourloaf.com/blog/privacy-settings-in-ios-10/

如果不是,那么应用程序会在您的情况下发生崩溃

如果您在ios 10中开发应用程序,则必须在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

iOS 10不允许访问联系人,相机,照片库,用户位置等,直到我们提到为什么我们使用它。打开你的plist作为源代码在dict下添加以下代码现在再次运行它。

  NSPhotoLibraryUsageDescription $(PRODUCT_NAME) photo use  NSCameraUsageDescription $(PRODUCT_NAME) camera use  NSLocationUsageDescription $(PRODUCT_NAME) location use  NSContactsUsageDescription $(PRODUCT_NAME) contact use