访问相机和照片库
Posted
技术标签:
【中文标题】访问相机和照片库【英文标题】:Access to Camera and PhotoLibrary 【发布时间】:2016-12-12 12:14:09 【问题描述】:在我的 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)
【问题讨论】:
【参考方案1】:在 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);
输出:
【讨论】:
【参考方案2】:如果你在iOS10上运行,你必须在Info.plist中添加访问相机的条目
把这个key放到Info.plist中
隐私相机使用说明
http://useyourloaf.com/blog/privacy-settings-in-ios-10/
如果不是,应用程序会像您的情况一样崩溃
【讨论】:
【参考方案3】:如果您在 ios 10 中开发应用程序,则必须在 info.plist 中添加隐私权限设置并描述您需要此隐私的地方。
隐私设置列表:
蓝牙共享 - NSBluetoothPeripheralUsageDescription
日历 – NSCalendarsUsageDescription
CallKit – NSVoIPUsageDescription
相机 - NSCameraUsageDescription
联系人 – NSContactsUsageDescription
健康 – NSHealthShareUsageDescription 和 NSHealthUpdateUsageDescription
HomeKit – NSHomeKitUsageDescription
位置 - NSLocationUsageDescription,NSLocationAlwaysUsageDescription,
NSLocationWhenInUseUsageDescription
媒体库 - NSAppleMusicUsageDescription
麦克风 – NSMicrophoneUsageDescription
运动 - NSMotionUsageDescription
照片 – NSPhotoLibraryUsageDescription
提醒——NSRemindersUsageDescription
语音识别——NSSpeechRecognitionUsageDescription
SiriKit – NSSiriUsageDescription
电视提供商 - NSVideoSubscriberAccountUsageDescription
【讨论】:
【参考方案4】:iOS 10 不允许访问联系人、相机、照片库、用户位置等,直到我们提到我们为什么使用它。打开您的 plist 作为源代码在 dict 下添加以下代码现在再次运行它。
<!-- ? Photo Library -->
<key>NSPhotoLibraryUsageDescription</key>
<string>$(PRODUCT_NAME) photo use</string>
<!-- ? Camera -->
<key>NSCameraUsageDescription</key>
<string>$(PRODUCT_NAME) camera use</string>
<!-- ? Location -->
<key>NSLocationUsageDescription</key>
<string>$(PRODUCT_NAME) location use</string>
<!-- ? Contacts -->
<key>NSContactsUsageDescription</key>
<string>$(PRODUCT_NAME) contact use</string>
【讨论】:
以上是关于访问相机和照片库的主要内容,如果未能解决你的问题,请参考以下文章
使用 IBM Worklight 访问本机相机、照片、日历和语音记录
swift 使用Swift 4从用户设备访问相机,照片库,视频和文件
通过 javascript 或任何其他语言访问相机和照片信息?