简体   繁体   中英

Upload multiple images to Google Drive using API/SDK (IOS, Swift)

I am developing an App that will ask for 5 images, assign them to the proper UIImageViewer for preview purposes, and then I would like to hit upload and all 5 images go to a team drive. At this point all of it works except the code snippet I have is only able to upload images if they are in the App's Documents Directory. I would like to reference back to the 5 previously chosen images and upload them to a team drive but I do not know how to go about that.

Each button has been assigned a tag number, in which will equal their designated UIImageViewer. I am trying to reference the other previously chosen images but an error says in the 'UploadAction' button "Cannot convert value of type 'UIImage' to expected argument type 'String'". Here is my code, any help would be greatly appreciated!! (This is in Swift in Xcode, I could not find any examples using swift to upload pictures using a phone camera or photo gallery)

import UIKit
import GoogleSignIn
import GoogleAPIClientForREST

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, GIDSignInUIDelegate, UIPickerViewDelegate, UITextFieldDelegate{
    // func numberOfComponents(in pickerView: UIPickerView) -> Int {
    //
    // }

    // func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
     //
    // }

    @IBOutlet weak var imageView1: UIImageView!
    @IBOutlet weak var imageView2: UIImageView!
    @IBOutlet weak var imageView3: UIImageView!
    @IBOutlet weak var imageView4: UIImageView!
    @IBOutlet weak var imageView5: UIImageView!
    @IBOutlet weak var resultsLabel: UILabel!

    var imagePicker = UIImagePickerController()
    var imagePicked = 0

    var cameraPicker = UIImagePickerController()
    var cameraPicked = 0

    fileprivate let service = GTLRDriveService()
    private var drive: ATGoogleDrive?

    override func viewDidLoad() {
        super.viewDidLoad()
        cameraPicker.delegate = self
        cameraPicker.sourceType = .camera
        cameraPicker.allowsEditing = false;     imagePicker.delegate = self
        imagePicker.sourceType = .savedPhotosAlbum
        imagePicker.allowsEditing = false;
        GIDSignIn.sharedInstance().uiDelegate = self;
        self.view.backgroundColor = UIColor.lightGray;

        setupGoogleSignIn()

        drive = ATGoogleDrive(service)
    }

    @IBAction func openCamera(_ sender: UIButton){
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera){
            cameraPicked = sender.tag
            present(cameraPicker, animated: true)
        }
    }

    @IBAction func chooseImage1(_ sender: UIButton) {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.savedPhotosAlbum){
            imagePicked = sender.tag
            present(imagePicker, animated: true)
        }
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage;

        let cameraImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage;

        if imagePicked == 1 {
            imageView1.image = pickedImage
        } else if imagePicked == 2 {
            imageView2.image = pickedImage
        } else if imagePicked == 3 {
            imageView3.image = pickedImage
        } else if imagePicked == 4 {
            imageView4.image = pickedImage
        } else if imagePicked == 5 {
            imageView5.image = pickedImage
        } else if cameraPicked == 6 {
            imageView1.image = cameraImage
        } else if cameraPicked == 7 {
            imageView2.image = cameraImage
        } else if cameraPicked == 8 {
            imageView3.image = cameraImage
        } else if cameraPicked == 9 {
            imageView4.image = cameraImage
        } else if cameraPicked == 10 {
            imageView5.image = cameraImage
        }
        dismiss(animated: true)
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true)
    }

    private func setupGoogleSignIn() {
        GIDSignIn.sharedInstance().delegate = self
        GIDSignIn.sharedInstance().uiDelegate = self
        GIDSignIn.sharedInstance().scopes = [kGTLRAuthScopeDriveFile]
        GIDSignIn.sharedInstance().signInSilently()
    }

    // MARK: - Actions
    **@IBAction func uploadAction(_ sender: Any) {
        if let documentsDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last {
            let uploadem = imageView1.image
            drive?.uploadFile("agostini_tech_demo", filePath: uploadem, MIMEType: "image/png") { (fileID, error) in
                print("Upload file ID: \(fileID); Error: \(error?.localizedDescription)")**
            }
        }
    }

    @IBAction func listAction(_ sender: Any) {
        drive?.listFilesInFolder("agostini_tech_demo") { (files, error) in
            guard let fileList = files else {
                print("Error listing files: \(error?.localizedDescription)")
                return
            }

            self.resultsLabel.text = fileList.files?.description
        }
    }
}

// MARK: - GIDSignInDelegate
extension ViewController: GIDSignInDelegate {
    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
       if let _ = error {
       //     service.authorizer = nil
        } else {
        //    service.authorizer = user.authentication.fetcherAuthorizer()
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM