Access to Camera and PhotoLibrary

╄→尐↘猪︶ㄣ 提交于 2019-12-02 20:56:05

问题


In my iOS app I have an ImageView and two Buttons for opening the camera and the photolibrary. When I click on one of the buttons the app closes. (I'm running the app on my device, not the simulator) What do I have to change in my code?

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:


In iOS 10 you need permission to access photoLibrary or camera by adding below keys to your plist and you need to use the proper delegate method.

To Access Photo Library:

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

To Access Device Camera:

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

To Pick and display Image:

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
    ImageDisplay.image = image
    }
    picker.dismiss(animated: true, completion: nil);
 }

Output:




回答2:


If you are running on iOS10, you have to add entries in Info.plist for accessing camera

put this key in Info.plist

Privacy-Camera Usage Description

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

If not the app would crash as is happening in your case




回答3:


If you are developing the app in ios 10 , then have to add the privacy permission setting in your info.plist and describe something where you need this privacy.

Privacy Setting List:

Bluetooth Sharing – NSBluetoothPeripheralUsageDescription

Calendar – NSCalendarsUsageDescription

CallKit – NSVoIPUsageDescription

Camera – NSCameraUsageDescription

Contacts – NSContactsUsageDescription

Health – NSHealthShareUsageDescription & NSHealthUpdateUsageDescription

HomeKit – NSHomeKitUsageDescription

Location – NSLocationUsageDescription, NSLocationAlwaysUsageDescription,

NSLocationWhenInUseUsageDescription

Media Library – NSAppleMusicUsageDescription

Microphone – NSMicrophoneUsageDescription

Motion – NSMotionUsageDescription

Photos – NSPhotoLibraryUsageDescription

Reminders – NSRemindersUsageDescription

Speech Recognition – NSSpeechRecognitionUsageDescription

SiriKit – NSSiriUsageDescription

TV Provider – NSVideoSubscriberAccountUsageDescription




回答4:


iOS 10 is not allowed to access the Contact, Camera, Photo library, User location and so on until we mention Why we are using it.open your plist as Source code add the below code under dict Now run it again.

<!-- 🖼 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>


来源:https://stackoverflow.com/questions/41100717/access-to-camera-and-photolibrary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!