Swift 3 and backendless - 我如何上传图片?

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

我正在使用 Swift 3 和 backendless 构建一个应用程序,但我无法从我的应用程序(用户将从相机或照片库拍摄照片)上传图像到 backendless。 APP正在保存图片到相册中:

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

}

截图地址是什么?

我如何将它上传到 backEndLess?

你应该使用Backendless.file.upload方法。

例如:

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)");
    })
}

如果您需要以 PNG 格式上传,请使用 UIImagePNGRepresentation instead of IImageJPEGRepresentation

要上传用户从保存的相册中直接选取的图片或相机拍摄的图片,您可以这样做:

  1. 使用UINavigationControllerDelegate & UIImagePickerControllerDelegate
  2. 指定要使用的UIImagePickerControllerSourceType,例如:.photoLibrary.camera.savedPhotosAlbum
  3. 在用户 didFinishPickingMediaWithInfo
  4. 时上传创建的 UIImage

举个例子:

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");
    });
}
}

还要记住,要使用那些你应该声明的:NSPhotoLibraryUsageDescription & NSCameraUsageDescription 在你 Info.plist 否则它会在 iOS 10 中崩溃,如链接参考中所述。

举个例子:

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