Search code examples
swiftswift2logicphotosframework

Trying to retrieve NSURL from imagepickercontroller with Photos Framework [Swift 2.0]


I am attempting to grab an NSURL from an image or video when a user selects it through a imagepickercontroller, using the Photos Framework

import Photos

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    let url: NSURL = (info["UIImagePickerControllerReferenceURL"] as! NSURL)
    mediaUploadView.image = info[UIImagePickerControllerOriginalImage]as? UIImage

    print(url)
    self.dismissViewControllerAnimated(true, completion: nil)
    return UIImagePickerControllerReferenceURL
}

I receive the error "unexpected non void return value in void function".

The end goal is to use the NSURL to upload the image / video / gif to a Firebase Database.

I'm honestly not sure if my logic here is correct, I'm still new to coding. Any help is greatly appreciated!


Solution

  • In swift, you have to say in the at the end of the function before the opening "{" what the return value is going to be, if you don't, then it is considered a void function. A void function means that it does not return any value. So, to tell it that it will return a NSURL, try this:

    import Photos
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) -> NSURL {
    
    let url: NSURL = (info[UIImagePickerControllerReferenceURL] as! NSURL)
    mediaUploadView.image = info[UIImagePickerControllerOriginalImage]as? UIImage
    
    print(url)
    self.dismissViewControllerAnimated(true, completion: nil)
    return url
    

    }

    Just copy and paste this, the only difference is that it tells the function what the return value will be, and will no longer return that error. I also suggest removing the "" around the

    (info[UIImagePickerControllerReferenceURL] as! NSURL)
    

    This will return a NSURL.