ndduong
ndduong

Reputation: 439

How do I get Image reference and upload image to Firebase?

I am new to iOS development and I am trying to use Firebase Storage service to store my images. I have the following methods in my app, but I am unable to locate the NSURL reference to the UIImage. I use the following methods to grab an image from the library.

@IBAction func getImage(sender: UIButton) {
    let image = UIImagePickerController()
    image.delegate = self
    image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    self.presentViewController(image, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    let theInfo: NSDictionary = info as NSDictionary
    let img: UIImage = theInfo.objectForKey(UIImagePickerControllerOriginalImage) as! UIImage
    imageView.image = img

    let localFile = info[UIImagePickerControllerReferenceURL] as! NSURL
    uploadToFireBase(localFile)
    self.dismissViewControllerAnimated(true, completion: nil)
}

And I use this methods to try and upload it to Firebase

func uploadToFireBase(localFile: NSURL) {
    let storageRef = FIRStorage.storage().referenceForURL("gs://logintest-90287.appspot.com/Pictures")
    let uploadTask = storageRef.putFile(localFile, metadata: nil) { metadata, error in
        if (error != nil) {
            // Uh-oh, an error occurred!
        } else {
            // Metadata contains file metadata such as size, content-type, and download URL.
            let downloadURL = metadata!.downloadURL
        }
    }
}

However, XCode keeps telling me "Body file is unreachable: /asset.JPG". I tried to grab the NSURL by using the following methods but it also does not work.

    let imageUrl          = info[UIImagePickerControllerReferenceURL] as! NSURL
    let imageName         = imageUrl.lastPathComponent
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String!
    let photoURL          = NSURL(fileURLWithPath: documentDirectory)
    let localPath         = photoURL.URLByAppendingPathComponent(imageName!)

Can someone please help and tell me how I can upload the image to Firebase?

Upvotes: 1

Views: 1761

Answers (1)

Sharud
Sharud

Reputation: 425

let localFile = info[UIImagePickerControllerReferenceURL] as! NSURL
let assets = PHAsset.fetchAssetsWithALAssetURLs([localFile], options: nil)
let imageURL = assets.firstObject?.fullSizeImageURL
uploadToFireBase(imageURL)

The localFile you get from info[UIImagePickerControllerReferenceURL] as! NSURL is is not an image url, but an Asset Library URL for the image. You then need to fetch the asset using the asset URL, and retrieve the URL to the full size image.

[[Update: 1]]

I could only get the path to work by going through requesting the photo as an editable object. Not sure why.

let referenceUrl = info[UIImagePickerControllerReferenceURL] as! NSURL
let assets = PHAsset.fetchAssetsWithALAssetURLs([referenceUrl], options: nil)
let asset = assets.firstObject
asset?.requestContentEditingInputWithOptions(nil, completionHandler: { (contentEditingInput, info) in
    let imageFile = contentEditingInput?.fullSizeImageURL

imageFile then has the absolute path to the photo on the device.

Upvotes: 1

Related Questions