Question

I am using the the STTwitter API to make an App only twitter Feed. I have successfully output the tweet to the table cell, but now I'm attempting to connect user profile images and I am running in to some problems. I tried implementing the code I found here, but I was getting an error stating "No known class method for selector 'imageWithContentsOfURL:'" so I fixed the problem by replacing UIImage with CIImage. However, now my app is crashing because I'm trying to output a CIImage to an UIImageView. My code and errors are as follows:

Code:

- (void)viewDidLoad
{
    [super viewDidLoad];


    NSString* boldFontName = @"Avenir-Black";

    [self styleNavigationBarWithFontName:boldFontName];

    self.title = @"Twitter Feed";

    self.feedTableView.dataSource = self;
    self.feedTableView.delegate = self;

    self.feedTableView.backgroundColor = [UIColor whiteColor];
    self.feedTableView.separatorColor = [UIColor colorWithWhite:0.9 alpha:0.6];

    //self.profileImages = [NSArray arrayWithObjects:@"profile.jpg", @"profile-1.jpg", @"profile-2.jpg", @"profile-3.jpg", nil];

    STTwitterAPI *twitter = [STTwitterAPI twitterAPIAppOnlyWithConsumerKey:@"stuff"
                                                            consumerSecret:@"stuff"];

    [twitter verifyCredentialsWithSuccessBlock:^(NSString *username) {

        [twitter getUserTimelineWithScreenName:@"RileyVLloyd"
                                  successBlock:^(NSArray *statuses) {
                                      self.twitterDataSource = [NSMutableArray arrayWithArray:statuses];

                                      for (int i=1; i <= _twitterDataSource.count; i++) {
                                      NSLog(@"%d", i);
                                      NSDictionary *tweetDictionary = self.twitterDataSource[i];
                                      NSString *final = tweetDictionary[@"profile_image_url"];
                                      NSLog(@"%@", final);
                                      }

                                      [self.feedTableView reloadData];
                                  } errorBlock:^(NSError *error) {
                                      NSLog(@"%@", error.debugDescription);
                                  }];

    } errorBlock:^(NSError *error) {
        NSLog(@"%@", error.debugDescription);
    }];
    //[self getTimeLine];

}



#pragma mark Table View Methods

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.twitterDataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID =  @"FeedCell3" ;

    FeedCell3 *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (cell == nil) {
        cell = [[FeedCell3 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }


    cell.nameLabel.text = @"RileyVLloyd";


    cell.likeCountLabel.text = @"293 likes";
    cell.commentCountLabel.text = @"55 comments";

    //NSString* profileImageName = self.profileImage[indexPath.row%self.profileImage.count];
    cell.profileImageView.image = _profileImage;

    NSInteger idx = indexPath.row;
    NSDictionary *t = self.twitterDataSource[idx];

    cell.updateLabel.text = t[@"text"];

    cell.dateLabel.text = @"1 day ago";

    return cell;
}
Was it helpful?

Solution

An easier way to accomplish this would be to use the SDWebImage API. The API asynchronously loads the image, so then you won't have to worry about loading due to the main thread being used for loading the images. Also, this API only requires a couple lines of code below to be added to your UITableViewCell method.

NSString *aURL = t[@"user"][@"profile_image_url"];
[cell.profileImageView setImageWithURL:[NSURL URLWithString:aURL]
              placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

OTHER TIPS

Your code does crash because you're trying to get a value for the key profile_image_url on username which is a string, hence the exception <__NSCFString ...> is not key value coding-compliant for the key profile_image_url.

Let's assume that what you really want to do here is retrieving the images for each tweet author.

You have to iterate over the statuses and, for each of them, extract the profile_image_url and create a UIImage with it.

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