SubReal888
SubReal888

Reputation: 13

Swift 3 and backendless - how do i upload an image?

I'm building an APP with Swift 3 and backendless and i'm having trouble to upload an image from my APP (the user will take a pic from camera or from photoLibrary) to backendless. The APP is saving the images to the photoLibrary :

func savePic() {
    let imageData = UIImageJPEGRepresentation(itemPic.image!, 0.5);
    let compressedJPEGImage = UIImage(data: imageData!);
    UIImageWriteToSavedPhotosAlbum(compressedJPEGImage!, nil, nil, nil);

}

What is the taken image address?

And how do i upload it to backEndLess?

Upvotes: 1

Views: 402

Answers (1)

Nikita Kurtin
Nikita Kurtin

Reputation: 6237

You should use Backendless.file.upload method.

For example:

func uploadImage(_ img:UIImage){
    //convert UIImage to jpg Data with 0.5 compressionQuality (use 1 for full quality)
    let jpg=UIImageJPEGRepresentation(img, 0.5);
    //saving file name with current timestamp
    let fileName="IMG_\(Date().timeIntervalSince1970).jpg";
    //uploading image asynchronously 
    Backendless.sharedInstance().file.upload("imagesFolder/\(fileName)", content:jpg, response: {(res) in
        //uploading image finished
    }, error: {(e) in
        //failed to upload
        print("error code: \(e!.faultCode)");
    })
}

If you need to upload it in PNG format use UIImagePNGRepresentation instead of IImageJPEGRepresentation

To upload an Image directly picked by user from saved photos album or taken by camera you can do it like this:

  1. Use UINavigationControllerDelegate & UIImagePickerControllerDelegate
  2. Specify UIImagePickerControllerSourceType to be used, such as: .photoLibrary, .camera or .savedPhotosAlbum
  3. Upload the created UIImage when user didFinishPickingMediaWithInfo

Here an example:

class MyViewController : UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate{

var imgPicker = UIImagePickerController();

var backendless = Backendless.sharedInstance();

override func viewDidLoad() {
    imgPicker.delegate = self;
}

@IBAction func imgFromCamera(_ btn:UIButton){
    imgPicker.sourceType = .camera;//image picker with camera
    show(imgPicker, sender: self);//open image picker
}

@IBAction func imgFromSavedPhotos(_ btn:UIButton){
    imgPicker.sourceType = .savedPhotosAlbum;//image picker from saved photos
    show(imgPicker, sender: self);//open image picker
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let img = info[UIImagePickerControllerOriginalImage] as! UIImage;
    uploadImage(img);
    dismiss(animated: true, completion: nil);
}

func uploadImage(_ img:UIImage){
    let jpg=UIImageJPEGRepresentation(img, 1);//full scale JPG
    //let png=UIImagePNGRepresentation(img);//full scale PNG
    let fileName="IMG_\(Date().timeIntervalSince1970).jpg";
    //upload img
    Backendless.sharedInstance().file.upload("imagesFolder/\(fileName)", content:jpg, response: {(res) in
     //successfully uploaded
    }, error: {(e) in
        //failed to upload
    })
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    dismiss(animated: false, completion: {
        print("user didn't pick any image");
    });
}
}

Also keep in mind that to use those you should declare: NSPhotoLibraryUsageDescription & NSCameraUsageDescription in you Info.plist otherwise it crash in iOS 10 as stated in linked reference.

Here an example:

<key>NSPhotoLibraryUsageDescription</key>
<string>Your explanation here</string>
<key>NSCameraUsageDescription</key>
<string>Your explanation here</string>

Upvotes: 3

Related Questions