hanumanDev
hanumanDev

Reputation: 6614

Post an image to Facebook from the iPhone camera library in iOS

I'm using the following method to post a photo to Facebook. It works fine, however the image is set like this: NSURL *url = [NSURL URLWithString:@"http://www.example.com/image.png"];

I'd like to have the user select the image from their iPhone camera folder instead. How would I go about doing this - retrieving an image from the Photo Album and sending it from this method?

thanks for any help

- (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];
}

Upvotes: 0

Views: 576

Answers (2)

Adil Soomro
Adil Soomro

Reputation: 37729

If you are done with selecting image from gallery?

if not then please follow the steps here in this blog entry, once you got the image, then simply do this way:

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

Upvotes: 1

toto
toto

Reputation: 455

For this you should use the UIImagePickerController. Take a look at the Apple docs here:

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

P.S.: -[NSData dataWithContentsOfURL:] is not a good idea for non file: URLs. It is synchronous. Rather use async API like -[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]

Upvotes: 1

Related Questions