Question

I have a "share on twitter" button action in my app.

I have a JSON image thats being parsed that I want to attach to the tweet. I'm having trouble accomplishing this. I tried converting the NSString to a UIImage but my code isn't working.

Any help?

- (IBAction)shareOnTwitter:(id)sender {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweetSheet = [SLComposeViewController
                                               composeViewControllerForServiceType:SLServiceTypeTwitter];

    NSString *thumbURL = _singleRelease[@"images"][0][@"image_file"][@"image_file"][@"medium"][@"url"];
    UIImage *image = [UIImage imageWithContentsOfFile:thumbURL];
    [tweetSheet addImage:image];

        [self presentViewController:tweetSheet animated:YES completion:nil];
    }
}
Was it helpful?

Solution

If the image is somewhere in your bundle, I'd try loading it up like this:

NSString *fileName = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:fileName];

But if you were grabbing some url from some JSON response from a server, I'd try loading it like this:

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:MyURL]]];

Perhaps you're going after the 2nd one?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top