how to add image from photo library or camera to my Ipad application?

后端 未结 3 1823
北海茫月
北海茫月 2021-01-15 13:28

I want to add images from photo library or by using camera to my ipad application. I had used same coding as iphone . But the application is crashing. I think this is not t

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-15 13:52

    for Swift 4

    get image from camera

    let picker = UIImagePickerController()
    picker.delegate = self
    picker.allowsEditing = true
    picker.sourceType = .camera
    self.present(picker, animated: true, completion: nil)
    

    get image from library

    let picker = UIImagePickerController()
    picker.delegate = self
    picker.allowsEditing = true
    picker.sourceType = .photoLibrary
    self.present(picker, animated: true, completion: nil)
    

    delegate function

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
      var result = false
    
      if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
          //do something to image here ...
    
          picker.dismiss(animated: true, completion: nil)
      }
    }
    

    In your view controller, don't forget to inherit class UIImagePickerControllerDelegate, UINavigationControllerDelegate

提交回复
热议问题