Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Adding screenshot to tweet

I would like to add a screenshot to a tweet sheet. I have written some code but it does not seem to be working. It launches the tweet sheet but does not take the screenshot.

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage*theImage=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSData*theImageData=UIImageJPEGRepresentation(theImage, 1.0 ); 
    [theImageData writeToFile:@"example.jpeg" atomically:YES];

    TWTweetComposeViewController *tweetsheet = [[TWTweetComposeViewController alloc] init];
    [tweetsheet setInitialText:@"Initial text for tweet"];
    [self presentModalViewController:tweetsheet animated:YES];
    [tweetsheet addImage:[UIImage imageNamed:@"example.jpeg"]];

1 Answer

Stone Preston
Stone Preston
42,016 Points

why not just add the image captured in the graphics context instead of saving it?

[tweetsheet addImage:theImage];

The device is not capturing the screenshot.