0

So I have a UIImagepickerController and I am trying to get the URL of an image.

Here is my ImagePicker code:

class ImagePicker: UIImagePickerController {

init(mediaTypes: [String], allowsEditing: Bool) {
    super.init(navigationBarClass: .none, toolbarClass: .none)
    self.mediaTypes = mediaTypes
    self.allowsEditing = allowsEditing
    self.imageExportPreset = .current        
    self.sourceType = .photoLibrary    
}
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

Here is how I call the image picker in the view controller:

    func presentImagePicker(type: [String]) {
    let picker = ImagePicker(mediaTypes: type, allowsEditing: true)
    picker.delegate = self
    present(picker, animated: true)
}

And:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    let imageURL = info[.imageURL] as? URL
    // This print statement returns nil.
    print("\(imageURL)")
}

It seems like it is working fine, but when I select my image in the image picker the imageURL returns nil and I don't know why.

Maybe this will also help, but when I switch the info[.imageURL] to info[.mediaURL] and change the image picker type to ["public.movie"] instead of ["public.image"], it works fine and the mediaURL does not return nil, but it returns a url.

So for some reason it will return a url for a movie, but it won't return a url for an image.

Trev347
  • 318
  • 1
  • 12

0 Answers0