首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >访问摄像头和PhotoLibrary

访问摄像头和PhotoLibrary
EN

Stack Overflow用户
提问于 2016-12-12 20:14:10
回答 4查看 2.1K关注 0票数 0

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

代码语言:javascript
复制
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)
}

}

EN

回答 4

Stack Overflow用户

发布于 2016-12-12 21:34:38

在iOS 10中,您需要权限才能访问photoLibrary或camera,方法是将以下键添加到您的plist中,并且您需要使用正确的委托方法。

访问图片库的

代码语言:javascript
复制
@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)
      } 
    }

访问设备摄像头的

代码语言:javascript
复制
@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)
        }
    }

用于拾取和显示图像的

代码语言:javascript
复制
 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
EN

Stack Overflow用户

发布于 2016-12-12 20:19:12

如果在iOS10上运行,则必须在Info.plist中添加用于访问照相机的条目

将此密钥放入Info.plist

隐私-摄像头使用说明

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

如果不是,应用程序将会崩溃,就像您的情况一样

票数 0
EN

Stack Overflow用户

发布于 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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41100717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档