我正在尝试加载并显示图像,然后显示到 tableview 单元格,该图像是来自谷歌的链接。我试图用于dispatch_async
轻松加载,但它不会加载所有图像。
这是我的代码的一部分:
-(void) buildCellView: (NSArray *) data andIndex: (NSInteger) index{
NSDictionary *comment = [data objectAtIndex:index];
cell.backgroundColor = [BaseView colorWithHexString:@"f3f3f3"];
commentsView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
commentUsersImg = [[UIImageView alloc] initWithFrame:CGRectMake(15, 8, 28, 28)];
if ([comment valueForKeyPath:@"image_url"] !=nil) {
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void){
NSURL * url = [[NSURL alloc] initWithString:[comment valueForKeyPath:@"image_url"]];
NSData * imageData = [[NSData alloc] initWithContentsOfURL:url] ;
dispatch_async(dispatch_get_main_queue(), ^(void){
// commentUsersImg.image = [UIImage imageWithData:imageData];
[[self commentUsersImg] setImage:[UIImage imageWithData:imageData]];
});
});
}
commentUsersImg.layer.cornerRadius = 14;
commentUsersImg.clipsToBounds = YES;
[commentsView addSubview:commentUsersImg];
这就是我得到的:
我将我的网址存储在一个数组中,这是我的数组数组的一部分:
[dummyData addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"http://g-ecx.images-amazon.com/images/G/01/electronics/detail-page/Klipsch-Image-S4-II-Black-Lifestyle.jpg", @"badge.png",@"Got Strike! at", @"Alex Shin",
[NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects:@"James", @"30 minutes ago", @"Great Shot!", @"https://c676132.ssl.cf0.rackcdn.com/_84A2468-50cf361795eb2.jpg", nil]
forKeys:
[NSArray arrayWithObjects:@"name", @"dateline", @"body", @"image_url", nil]],
[NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects:@"Kim", @"10 minutes ago", @"I should have seen it.", @"http://3.bp.blogspot.com/-tBFQC9y-ZpM/Tmb8DaeYEmI/AAAAAAAAA9M/TahVx2IiQsE/s400/Bowling+and+Best+Bowling+Player+Shalin+Zulkifli+cover.jpg", nil]
forKeys:
[NSArray arrayWithObjects:@"name", @"dateline", @"body", @"image_url", nil]],
[NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects:@"Matt", @"5 minutes ago", @"Nice", @"http://www.beverlyhillsmagazine.com/wp-content/uploads/Ashton-Kutcher-Hollywood-Stars-Rich-and-Famous-Movie-Stars-Star-of-the-Week-Beverly-Hills-Magazine-Hollywood-Magazines--300x336.jpg", nil]
forKeys:
[NSArray arrayWithObjects:@"name", @"dateline", @"body", @"image_url", nil]]
, nil] , nil]
forKeys:[NSArray arrayWithObjects:@"photo", @"credit",@"credit_label", @"name", @"comments", nil]
]];