我正在开发一个iOS应用程序。并通过JSON获取图像并在桌面上显示。使用此方法dispatch_async(kBgQueue, ^{...
和图像显示在桌面视图上,但所有图像都显示在一个单元格上并自动回收。图像可能会在一段时间后改变,也可以使用占位符图像。使用两个字符串显示图像一个字符串可用于图像名称和第二个字符串可用于URL。然后连接字符串以显示在收集视图中相同问题的图像。任何其他方法在tableview和collection视图中快速显示图像。
我的代码:加载JSON图像快速
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
mycell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
UILabel *mylabel = (UILabel *)[mycell viewWithTag:100];
NSDictionary *dic=[str objectAtIndex:indexPath.row];
GRLanguage *sharedManager = [GRLanguage sharedManager];
if ([sharedManager.LanguageCode isEqual:@"hi"]) {
mylabel.text=[dic objectForKey:@"hindi_name"];
}else if ([sharedManager.LanguageCode isEqual:@"pa"])
{
mylabel.text=[dic objectForKey:@"punjabi_name"];
}else
{
mylabel.text=[dic objectForKey:@"name"];
}
[mycell setBackgroundColor:[UIColor clearColor]];
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back copy.png"]];
self.tableview.backgroundView =img;
dispatch_async(kBgQueue, ^{ //Using For Lazy Loading
UIImageView *imagetlt = (UIImageView *)[mycell viewWithTag:1];
NSString *image =[dic objectForKey:@"image_path"];
NSString *[email protected]"http://webapp.opaxweb.net/images/";
path=[path stringByAppendingString:image];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:path]];
if (image == nil) {
[imagetlt setImage:[UIImage imageNamed:@"rel_circle.png"]];
}else
{
dispatch_async(dispatch_get_main_queue(), ^{
// UITableViewCell*img = (id) [tableView cellForRowAtIndexPath:indexPath];
imagetlt.image = [UIImage imageWithData: imageData];
});
}
});
return mycell;
}
建议 - 在GitHub中的AsyncImageLoader。 –
使用AFNetworking .. –