Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


How To Select The Image From Library ?

Hello guys, i'm working on my test project with twitter API. Could anybody give me an idea how to select an image to upload to twitter with a status message using their update with media API ( https://dev.twitter.com/docs/api/1.1/post/statuses/update_with_media ) . I already successfully upload an image by getting image url from the web and parse it to twitter API. Now i want to select the image from the library or take a new photo to make a new twitter status. btw, here is my code on implementation file.

#import "ViewController.h"
#import <Social/Social.h>
#import <Accounts/Accounts.h>

@interface ViewController ()

@property (nonatomic) ACAccountStore *accountStore;


@implementation ViewController

- (void)viewDidLoad
    [super viewDidLoad];
  NSURL *imageURL = [NSURL URLWithString:@"https://www.google.com/images/srpr/logo11w.png"];
    NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
    UIImage *image = [UIImage imageWithData:imageData scale:10.0f];
    [self postImage:image withStatus:@"googletest"];

- (void)postImage:(UIImage *)image withStatus:(NSString *)status {

    NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update_with_media.json"];
    NSMutableDictionary *paramater = [[NSMutableDictionary alloc] init];

    //set the parameter here. to see others acceptable parameters find it at twitter API here : http://bit.ly/Occe6R
    [paramater setObject:status forKey:@"status"];

    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
        if (granted == YES) {

            NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];

            if ([accountsArray count] > 0) {
                ACAccount *twitterAccount = [accountsArray lastObject];

                SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:url parameters:paramater];

                NSData *imageData = UIImageJPEGRepresentation(image, 1.f);
                [postRequest addMultipartData:imageData withName:@"media[]" type:@"image/jpeg" filename:@"image.jpg"];

                [postRequest setAccount:twitterAccount]; // or  postRequest.account = twitterAccount;

                [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                    NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];
                    NSLog(@"output = %@",output);
                    dispatch_async(dispatch_get_main_queue(), ^{





2 Answers

Thank you. I will try to understand and try to do it.