Steve
Steve

Reputation: 151

Share screenshot on Facebook

I am using the following code to take a screenshot on my iPhone and save it to the photo album. What I would like is a share button to automatically send this screenshot and to Facebook. Does anyone have any ideas how to implement?

- (UIImage*)captureView:(UIView *)view
{
CGRect rect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}

- (void)saveScreenshotToPhotosAlbum:(UIView *)view
{
UIImageWriteToSavedPhotosAlbum([self captureView:self.view], nil, nil, nil);
}

- (void)viewDidLoad
{
[super viewDidLoad];
[self setUpData];

CGRect frame = self.view.bounds;
frame.size.height -= 90;

self.tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
[self.tableView setBackgroundColor:[UIColor clearColor]];
[self.tableView setDataSource:self];
[self.tableView setDelegate:self];

[self.view addSubview:self.tableView];

UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(saveScreenshotToPhotosAlbum:)];
[self.navigationItem setRightBarButtonItem:shareBtn];

}

Upvotes: 1

Views: 2824

Answers (1)

Nikola Kirev
Nikola Kirev

Reputation: 3152

Well, you can use the iOS6 built-in Facebook integration. Include the Social framwork in Build Phases The code should look bi similar to this snipped:

if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]){
    mySLComposerSheet = [[SLComposeViewController alloc] init];
    mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    [mySLComposerSheet setInitialText:@"Some Text"];
    [mySLComposerSheet addImage:[self captureView:self.view]];
    [self presentViewController:mySLComposerSheet animated:YES completion:nil];
}

*Note this will not "automatically" share the screenshot but it will attach it to a Facebook Compose Sheet and the user can post it if he/she wants to. To do it automatically, you will need to do a deeper Facebook integration and the user should give permission for your app to post on his/her wall. This again should be done with the Social framework or ShareKit if the project Deployment target is bellow iOS 6.0

Upvotes: 3

Related Questions