Trying to update UIImageView with image from photo library/camera. Seems like some error relating to permission accessing the camera/photo library
Full error: discovery] errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}
import Photos
import UIKit
import MapKit
class detailViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var selectedName: String?
var selectedImage: UIImage?
var selectedText: String?
@IBOutlet weak var cityName: UILabel!
@IBOutlet weak var cityImage: UIImageView!
@IBOutlet weak var cText: UILabel!
@IBOutlet weak var map: MKMapView!
@IBOutlet weak var lat: UILabel!
@IBOutlet weak var lon: UILabel!
@IBOutlet weak var searchText: UITextField!
@IBOutlet weak var img_view: UIImageView!
let imagePickerController = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
self.cityName.text = selectedName!
self.cityImage.image = selectedImage!
self.cText.text = selectedText!
imagePickerController.delegate = self
}
var picker = UIImagePickerController()
@IBAction func pickButton(_ sender: UIButton) {
checkPermission()
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Take Photo", style: .default, handler: { _ in
self.camera()
}))
alert.addAction(UIAlertAction(title: "Select Photo", style: .default, handler: { _ in
self.gallery()
}))
alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
func camera(){
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)){
picker.sourceType = UIImagePickerControllerSourceType.camera
picker.allowsEditing = true
picker.delegate = self
self.present(picker, animated: true, completion: nil)
}
else{
let alert = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
func gallery(){
picker.sourceType = UIImagePickerControllerSourceType.photoLibrary
picker.allowsEditing = true
picker.delegate = self
self.present(picker, animated: true, completion: nil)
}
func checkPermission() {
let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
switch photoAuthorizationStatus {
case .authorized:
print("Access is granted by user")
case .notDetermined:
PHPhotoLibrary.requestAuthorization({
(newStatus) in
print("status is \(newStatus)")
if newStatus == PHAuthorizationStatus.authorized {
print("success")
}
})
print("It is not determined until now")
case .restricted:
print("User do not have access to photo album.")
case .denied:
print("User has denied the permission.")
}
}
}