tableViewがあり、URLから画像を読み込む必要があります。画像のURLを含む配列があり、ページが読み込まれるときに、すべての画像をテーブルビューに読み込む必要があります。単一のURLからではなく、異なるURLの配列があることに注意してください。どうすればそれを実装できますか?助けてください
ありがとう。
tableViewがあり、URLから画像を読み込む必要があります。画像のURLを含む配列があり、ページが読み込まれるときに、すべての画像をテーブルビューに読み込む必要があります。単一のURLからではなく、異なるURLの配列があることに注意してください。どうすればそれを実装できますか?助けてください
ありがとう。
GCDを使用して、次のようにバックグラウンドスレッドで画像を読み込むことができます。
//get a dispatch queue
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//this will start the image loading in bg
dispatch_async(concurrentQueue, ^{
NSData *image = [[NSData alloc] initWithContentsOfURL:imageURL];
//this will set the image when loading is finished
dispatch_async(dispatch_get_main_queue(), ^{
imageView.image = [UIImage imageWithData:image];
});
});
やあ。ただし、おそらく、dispatch_release(concurrentQueue);を追加する必要があります。漏れがないことを確認します。–フランク8月25日19:43
インターネットから画像をダウンロードする場合は、遅延読み込みを使用できます
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//All you reusable cell implementation here.
//Since your Images sizes differ. Keep a custom Imageview
if(![imagesForCategories containsObject:indexPath])
{
customImageView.image = [UIImage imageNamed:@"default-image.png"];
NSMutableArray *arr = [[NSArray alloc] initWithObjects:[imageUrlArray objectAtIndex:indexPath.row],indexPath, nil];
[self performSelectorInBackground:@selector(loadImageInBackground:) withObject:arr];
[arr release];
}
return cell;
}
- (void) loadImageInBackground:(NSArray *)urlAndTagReference
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *imgUrl=[[NSURL alloc] initWithString:[urlAndTagReference objectAtIndex:0]];
NSData *imgData = [NSData dataWithContentsOfURL:imgUrl];
UIImage *img = [UIImage imageWithData:imgData];
[imgUrl release];
NSMutableArray *arr = [[NSMutableArray alloc ] initWithObjects:img,[urlAndTagReference objectAtIndex:1], nil ];
[self performSelectorOnMainThread:@selector(assignImageToImageView:) withObject:arr waitUntilDone:YES];
[arr release];
[pool release];
}
- (void) assignImageToImageView:(NSMutableArray *)imgAndTagReference
{
[imagesForCategories addObject:[imgAndTagReference objectAtIndex:1]];
UITableViewCell *cell = [celebCategoryTableView cellForRowAtIndexPath:[imgAndTagReference objectAtIndex:1]];
UIImageView *profilePic = (UIImageView *)[cell.contentView viewWithTag:20];
profilePic.image = [imgAndTagReference objectAtIndex:0];
}
SDWebImageを使用すると、UITableViewでの画像の読み込みが非常に簡単で高速になります。
https://github.com/rs/SDWebImage
このコードを試してください。imagearrayには画像のURLが含まれています
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [imagearray objectAtIndex:row]]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
cell.imageView.image =image;
return cell;
}
遅延読み込み用にカスタムセルを作成する必要があります。これにより、画像をフリーズすることなく正しくダウンロードできます。これを行う方法の良い例は次のとおりです。 非同期画像の読み込み
afnetworkiを使用すると、簡単すぎます。
//afnetworking
#import "UIImageView+AFNetworking.h"
[cell.iboImageView setImageWithURL:[NSURL URLWithString:server.imagen] placeholderImage:[UIImage imageNamed:@"qhacer_logo.png"]];