我正在使用STTwitter API制作仅限应用程序的Twitter Feed。我已成功将推文输出到表格单元格,但现在我正在尝试连接用户配置文件图像,我正在遇到一些问题。我尝试实现我发现here的代码,但是我收到一条错误,指出“没有已知的选择器类方法'imageWithContentsOfURL:'”所以我通过用CIImage替换UIImage来修复问题。但是,现在我的应用程序崩溃了,因为我正在尝试将CIImage输出到UIImageView。我的代码和错误如下:
代码:
- (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;
}
答案 0 :(得分:2)
更简单的方法是使用SDWebImage API。 API异步加载图像,因此您不必担心由于主线程用于加载图像而导致加载。此外,此API仅需要将几行代码添加到您的UITableViewCell方法中。
NSString *aURL = t[@"user"][@"profile_image_url"];
[cell.profileImageView setImageWithURL:[NSURL URLWithString:aURL]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
答案 1 :(得分:1)
您的代码确实崩溃了,因为您尝试获取profile_image_url
上的密钥username
的值,这是一个字符串,因此异常为<__NSCFString ...> is not key value coding-compliant for the key profile_image_url
。
让我们假设您真正想要做的就是为每个推文作者检索图像。
您必须遍历状态,并为每个状态提取profile_image_url
并使用它创建UIImage
。