【问题标题】: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)
}

}

【问题讨论】:

    标签: ios swift camera photolibrary


    【解决方案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>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-02-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-09-07
            • 1970-01-01
            • 1970-01-01
            • 2014-12-18
            相关资源
            最近更新 更多