如何将照片保存到iOS中的Google驱动器上的文件夹中

时间:2015-10-07 06:14:44

标签: ios objective-c xcode

我正在使用IOS 8开发一个应用程序,我将照片保存到谷歌驱动器。我已完成在Google云端硬盘上保存照片和创建文件夹。但无法将照片保存到谷歌硬盘上的文件夹中。

请帮我将照片保存到google drive的文件夹中。提前谢谢。

以下是创建文件夹

的代码
GTLDriveFile *folder = [GTLDriveFile object];
    folder.title = @"xyz";
    folder.mimeType = @"application/vnd.google-apps.folder";
    //folder.parents = @[parent];
    GTLQueryDrive *query = [GTLQueryDrive queryForFilesInsertWithObject:folder uploadParameters:nil];
    [self.service executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
                                                         GTLDriveFile *updatedFile,
                                                         NSError *error) {
        if (error == nil) {
            NSLog(@"Created folder");
        } else {
            NSLog(@"An error occurred: %@", error);
        }
    }]; 

以下是将照片保存到Google云端硬盘的代码

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

                       float actualHeight = image.size.height;


                       float actualWidth = image.size.width;

                       UIImage *myResizedImage ;
                       if (actualHeight>500 || actualWidth>500) {
                           myResizedImage = [ImageUtilities imageWithImage:image
                                                                   scaledToMaxWidth:300
                                                                          maxHeight:300];
                       }
                       else
                       {
                           myResizedImage=image;
                       }



    NSData *data = UIImagePNGRepresentation((UIImage *)myResizedImage);



        GTLUploadParameters *uploadParameters = [GTLUploadParameters uploadParametersWithData:data MIMEType:file.mimeType];

    GTLQueryDrive *query = [GTLQueryDrive queryForFilesInsertWithObject:file
                                                       uploadParameters:uploadParameters];
    GTLDriveParentReference *parentReference = [GTLDriveParentReference object];
    parentReference.identifier = @"root";
    file.parents = @[parentReference];


    NSLog(@"%@",query.q);
  //  UIAlertView *waitIndicator = [self showWaitIndicator:@"Uploading to Google Drive"];
     dispatch_async(dispatch_get_main_queue(), ^{
    [self.service executeQuery:query
                  completionHandler:^(GTLServiceTicket *ticket,GTLDriveFile *insertedFile, NSError *error)
     {
         //[waitIndicator dismissWithClickedButtonIndex:0 animated:YES];
         if (error == nil)
         {


             NSMutableDictionary *value=[[NSMutableDictionary alloc]init];

             [value setValue:rowidstring forKey:@"rowid"];

             [value setValue:@"1" forKey:@"status"];
             NSLog(@"insertedFile.identifier %@",insertedFile.identifier);
             // [self showAlert:@"Google Drive" message:@"File saved!"];
             [value setValue:insertedFile.identifier  forKey:@"googleid"];
             [[sqlitedatabase shardclass]updateCustomerphotodata:value];






         }
         else
         {
             NSLog(@"An error occurred: %@", error);
             [self showAlert:@"Google Drive" message:@"Sorry, an error occurred!"];
         }
     }];
      });

1 个答案:

答案 0 :(得分:1)

根据Google Developers网站Working With Folders

  

要在文件夹中创建文件,请在文件上设置parents属性   如下例所示:

在文件夹中创建文件夹

  

默认情况下,新创建的文件和文件夹将添加到根目录中   夹。要在其他文件夹中创建文件夹,请先检索   父文件夹获取其ID;然后你可以使用该ID来设置   新文件夹上的parents属性,如下所示:

GTLServiceDrive *drive = ...;
NSString *parentId = ...;

GTLDriveParentReference *parent = [GTLDriveParentReference object];
parent.identifier = parentId;

GTLDriveFile *folder = [GTLDriveFile object];
folder.title = @"My Application Files";
folder.mimeType = @"application/vnd.google-apps.folder";
folder.parents = @[parent];

GTLQueryDrive *query = [GTLQueryDrive queryForFilesInsertWithObject:folder uploadParameters:nil];
// ... execute query