首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在iOS中通过STTwitter获取轮廓图像

在iOS中通过STTwitter获取轮廓图像
EN

Stack Overflow用户
提问于 2014-05-06 01:56:32
回答 2查看 1.1K关注 0票数 0

我正在使用STTwitter API来制作一个仅限应用程序的twitter。我已经成功地将tweet输出到表单元格中,但是现在我试图连接用户配置文件图像,并且遇到了一些问题。我尝试实现我找到的这里代码,但是我遇到了一个错误:“选择器‘imageWithContentsOfURL:’没有已知的类方法”,所以我用CIImage替换UIImage解决了这个问题。然而,现在我的应用程序崩溃了,因为我试图将CIImage输出到UIImageView。我的代码和错误如下:

代码:

代码语言:javascript
复制
- (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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-07 05:18:58

实现这一目标的一种更简单的方法是使用SDWebImage API。API异步加载映像,因此您不必担心加载,因为主线程用于加载映像。此外,这个API只需要将下面的几行代码添加到UITableViewCell方法中。

代码语言:javascript
复制
NSString *aURL = t[@"user"][@"profile_image_url"];
[cell.profileImageView setImageWithURL:[NSURL URLWithString:aURL]
              placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
票数 2
EN

Stack Overflow用户

发布于 2014-05-06 11:37:23

您的代码确实崩溃了,因为您试图为键profile_image_urlusername上获取一个值,它是一个字符串,因此是异常<__NSCFString ...> is not key value coding-compliant for the key profile_image_url

让我们假设您真正想要做的是检索每个推特作者的图片。

您必须迭代这些状态,并对每个状态提取profile_image_url并使用它创建一个UIImage

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23484787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档