如何在iOS应用中使用GData框架将图像上传到Picasa?

时间:2014-02-19 07:32:19

标签: ios gdata picasa

在我的应用中,我有一个imageView,我想将图片发布到Picasa 我有一些建议使用GData Framwork。我是从

下载的
  

code.google.com/p/gdata-objectivec-client/downloads/list

但是,当我在Build阶段添加.a文件时,它显示为红色。我已经在SO以及其他论坛中尝试了所有方法 还有其他方法可以将图像上传到Picasa 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我没有应用程序来测试它,但我已经从this SO question翻译了php-curl解决方案。

    NSString *authCode = [NSString stringWithFormat:@"GoogleLogin auth=",@"ACCESS CODE FROM OAUTH2"];
    NSString *userId = @"USER ID GOES HERE";
    NSString *albumId = @"ALBUM ID GOES HERE";
    NSString *albumUrl = [NSString stringWithFormat:@"https://picasaweb.google.com/data/feed/api/user/%@/albumid/%@",userId, albumId];


    // To get the data from a PNG file
    NSData *postData = UIImagePNGRepresentation([UIImage imageNamed:@"YOUR IMAGE"]);

    // To get the data from a JPEG file
    //NSData *postData = UIImageJPEGRepresentation([UIImage imageNamed:@"YOUR IMAGE"], 0.9f);

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    // Init and set fields of the URLRequest
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setHTTPMethod:@"POST"];
    [request setURL:[NSURL URLWithString:[NSString stringWithString:albumUrl]]];
    [request setValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"2" forHTTPHeaderField:@"GData-Version"];
    [request setValue:authCode forHTTPHeaderField:@"Authorization"];
    [request setHTTPBody:postData];

    NSURLResponse* rep = nil;
    NSError *err = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&rep error:&err];

    if (data && !err) {
        //Do something with the response
    }

希望这会有所帮助。