简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM