6

tableViewがあり、URLから画像を読み込む必要があります。画像のURLを含む配列があり、ページが読み込まれるときに、すべての画像をテーブルビューに読み込む必要があります。単一のURLからではなく、異なるURLの配列があることに注意してください。どうすればそれを実装できますか?助けてください

ありがとう。

4

6 に答える 6

34

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

于 2011-09-27T06:43:43.057 に答える
7

インターネットから画像をダウンロードする場合は、遅延読み込みを使用できます

- (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];

}
于 2011-09-27T07:10:59.767 に答える
5

SDWebImageを使用すると、UITableViewでの画像の読み込みが非常に簡単で高速になります。
https://github.com/rs/SDWebImage

于 2011-09-27T07:49:19.843 に答える
4

このコードを試してください。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;
}
于 2011-09-27T06:41:05.573 に答える
3

遅延読み込み用にカスタムセルを作成する必要があります。これにより、画像をフリーズすることなく正しくダウンロードできます。これを行う方法の良い例は次のとおりです。 非同期画像の読み込み

于 2011-09-27T07:00:09.580 に答える
-2

afnetworkiを使用すると、簡単すぎます。

//afnetworking

#import "UIImageView+AFNetworking.h"

 [cell.iboImageView setImageWithURL:[NSURL URLWithString:server.imagen] placeholderImage:[UIImage imageNamed:@"qhacer_logo.png"]];
于 2015-01-08T00:19:08.003 に答える