objective-c 图片未出现在自定义 UITableViewCell 中
objective-c image not appearing in custom UITableViewCell
我有 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";
}
这是我的看法。
有人能帮我解决这个问题
您必须首先从 URL 加载图像内容:
- (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
这样的任何图像缓存库
它的图像url...你不能直接通过图像名称设置图像...你必须像这样从url获取数据...
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]];
请NSLog你的图片,以及imageView的frame。
而且 [UIImage imageNamed:NewsImageName ];
只从主包加载,看起来你正在从互联网加载图像。
另一种可能是你设置错误的框架导致它消失了。
你应该这样写
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
UIImage *img = [[UIImage alloc] initWithData:data];
试试这个
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL: NewsImageName]];
这里您的数据源联系人[@"featured_image"]是url。所以你必须从 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];
}
}];
我有 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";
}
这是我的看法。
您必须首先从 URL 加载图像内容:
- (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
这样的任何图像缓存库它的图像url...你不能直接通过图像名称设置图像...你必须像这样从url获取数据...
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]];
请NSLog你的图片,以及imageView的frame。
而且 [UIImage imageNamed:NewsImageName ];
只从主包加载,看起来你正在从互联网加载图像。
另一种可能是你设置错误的框架导致它消失了。
你应该这样写
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
UIImage *img = [[UIImage alloc] initWithData:data];
试试这个
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL: NewsImageName]];
这里您的数据源联系人[@"featured_image"]是url。所以你必须从 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];
}
}];