从用户提供的特定文件夹中的IOS应用程序上载Google云端硬盘上的图像

时间:2014-05-20 08:35:57

标签: ios iphone google-drive-api

我正在使用iOS应用程序,我必须从我的IOS应用程序上传Google Drive上的图像。 我正在使用下面的代码,但没有将我的图像插入特定文件夹。

- (void)uploadPhoto:(UIImage*)image
{

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"'Quickstart Uploaded File ('EEEE MMMM d, YYYY h:mm a, zzz')"];

    GTLDriveFile *file = [GTLDriveFile object];
    file.title = [dateFormat stringFromDate:[NSDate date]];
    file.descriptionProperty = @"Uploaded from the Google Drive iOS Quickstart";
    file.mimeType = @"image/png";

    NSData *data = UIImagePNGRepresentation((UIImage *)image);
    GTLUploadParameters *uploadParameters = [GTLUploadParameters uploadParametersWithData:data MIMEType:file.mimeType];
   GTLQueryDrive *query = [GTLQueryDrive queryForFilesInsertWithObject:file
                                                       uploadParameters:uploadParameters];
    GTLDriveParentReference *parentReference = [GTLDriveParentReference object];
    // parentReference.identifier = @"root";
  parentReference.identifier = [NSString stringWithFormat:@"root%@",albumNAME];

  file.parents = @[parentReference];


    UIAlertView *waitIndicator = [self showWaitIndicator:@"Uploading to Google Drive"];

    [self.driveService executeQuery:query
                  completionHandler:^(GTLServiceTicket *ticket,
                                      GTLDriveFile *insertedFile, NSError *error) {
                      [waitIndicator dismissWithClickedButtonIndex:0 animated:YES];
                      if (error == nil)
                      {
                          NSLog(@"File ID: %@", insertedFile.identifier);
                          [self showAlert:@"Google Drive" message:@"File saved!"];
                      }
                      else
                      {
                          NSLog(@"An error occurred: %@", error);
                          [self showAlert:@"Google Drive" message:@"Sorry, an error occurred!"];
                      }
                  }];

     }

albumNAME是我必须在google驱动器上插入图片的特定文件夹的名称。 请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

GTLDriveParentReference.identifier应该是文件夹的ID,而不是它的名称。您必须先找到或创建该文件夹,然后在此处使用该文件夹的ID。