我有NSMutableArray,它包含字符串,日期时间和UIImage
。 一切都显示在我的UITableView中,没有图片。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier =@"Cell";
CustomCell *customCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
contact = res[indexPath.row];
NSString *titleNews = contact[@"title"];
NSString *newsDateTime = contact[@"datetime"];
NSString *NewsImageName = contact[@"featured_image"];
UIImage *image = [UIImage imageNamed:NewsImageName ];
customCell.customFirstNameLabel.text = titleNews;
customCell.customLastnameLabel.text = newsDateTime;
customCell.customImageView.image =image;
return customCell;
}
我正在尝试从这里显示数据。
{
datetime = "2015-09-10 13:35:33";
"featured_image" = "http://198.72.115.125/~pratidin/assets/news_images/2015/09/10/thumbnails/bdp-001.jpg";
"item_id" = 102235;
"main_news_url" = "http://198.72.115.125/~pratidin/api/detailnews/102235";
"main_url" = "http://198.72.115.125/~pratidin/api/topnews";
summery = "\U0997\U09a4 \U0995\U09af\U09bc\U09c7\U0995\U09ae\U09be\U09b8 \U09af\U09be\U09ac\U09a4 \U09ae\U09cb\U09ac\U09be\U0987\U09b2\U09aa\U09cd\U09b0\U09c7\U09ae\U09c0\U09b0\U09be \U0985\U09aa\U09c7\U0995\U09cd\U09b7\U09be\U09af\U09bc…";
title = "\U098f\U09ac\U09be\U09b0 \U0986\U0987\U09ab\U09cb\U09a8 \U09ec\U098f\U09b8 \U0993 \U09ec\U098f\U09b8 \U09aa\U09cd\U09b2\U09be\U09b8";
}
答案 0 :(得分:3)
其图片网址...您无法通过图片名称直接设置图片...您必须从此网址获取数据......
NSURL *imgURL = [NSURL URLWithString:NewsImageName];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSData *imgData = [NSData dataWithContentsOfURL:imgURL];
dispatch_async(dispatch_get_main_queue(), ^{
imageView.image = [UIImage imageWithData:imgData];
});
});
或直接设置
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL: NewsImageName]];
答案 1 :(得分:2)
此处您的数据源联系人[@" featured_image"]是一个网址。所以你必须从url加载图像。您可以使用SDWebImage.framework加载图像
[cell.imageview sd_setImageWithURL:[NSURL URLWithString:contact[@"featured_image"]] placeholderImage:nil options:SDWebImageCacheMemoryOnly completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (image) {
[cell.imageview setImage:image];
}
}];
答案 2 :(得分:1)
您必须先从网址加载图片内容:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier =@"Cell";
CustomCell *customCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
contact = res[indexPath.row];
NSString *titleNews = contact[@"title"];
NSString *newsDateTime = contact[@"datetime"];
NSString *NewsImageName = contact[@"featured_image"];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:NewsImageName]]];
//UIImage *image = [UIImage imageNamed:NewsImageName ];
customCell.customFirstNameLabel.text = titleNews;
customCell.customLastnameLabel.text = newsDateTime;
customCell.customImageView.image =image;
return customCell;
}
请注意,这将是图像的同步提取。我建议你查看LazyTableImages函数:https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html 或任何图像缓存库,如https://github.com/rs/SDWebImage
答案 3 :(得分:1)
请NSLog您的图像和imageView的框架。
而[UIImage imageNamed:NewsImageName ];
仅从主要捆绑加载,看起来您正在从互联网上加载图片。
另一种可能是您设置了错误的框架,使其消失。
你应该以这种方式写作
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
UIImage *img = [[UIImage alloc] initWithData:data];
答案 4 :(得分:1)
试试这个
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL: NewsImageName]];