将图像从iOS中的iPhone相机库发布到Facebook

时间:2012-05-24 10:58:50

标签: iphone objective-c ios facebook-graph-api

我正在使用以下方法将照片发布到Facebook。它工作正常,但图像设置如下:NSURL *url = [NSURL URLWithString:@"http://www.example.com/image.png"];

我想让用户从他们的iPhone相机文件夹中选择图像。我将如何进行此操作 - 从相册中检索图像并从此方法发送图像?

感谢您的帮助

- (void)apiGraphUserPhotosPost {
    [self showActivityIndicator];
    currentAPICall = kAPIGraphUserPhotosPost;
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    // Download a sample photo
    NSURL *url = [NSURL URLWithString:@"http://www.example.com/image.png"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *img  = [[UIImage alloc] initWithData:data];
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   img, @"picture",
                                   nil];
    [img release];

    [[delegate facebook] requestWithGraphPath:@"me/photos"
                          andParams:params
                      andHttpMethod:@"POST"
                        andDelegate:self];
}

2 个答案:

答案 0 :(得分:1)

为此你应该使用UIImagePickerController。看看这里的Apple文档:

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html

P.S。:-[NSData dataWithContentsOfURL:]对于非file:网址不是一个好主意。它是同步的。而是使用像-[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]

这样的异步API

答案 1 :(得分:1)

如果您已完成从图库中选择图片?

如果没有,请按照步骤here in this blog entry获取图片,然后按照以下方式操作:

UIImage *img  = yourImageFromLibrary;
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   img, @"picture",
                                   nil];
... //rest of code