6

UITableViewからいくつかの画像をロードするアプリに がありますNSDocumentDirectory。それは機能しますが、上下にスクロールすると、アプリが少しフリーズするように見えます。これは、おそらくメインスレッドで画像が提供されているためであり、ロードされるまで tableView のスクロールを効果的にブロックします。私の問題は、スクロール中に後でそれらをロードする方法がわからないことです。これは「遅延ロード」機能です。

これは、画像をロードするために使用されるコード スニペットです。

imagesPath = [NSString stringWithFormat:@"%@/images/", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
if ([fileManager fileExistsAtPath:[imagesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%d.png", rowID]]]) {
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:[imagesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%d.png", rowID]]];
    // If image contains anything, set cellImage to image. If image is empty, use default, noimage.png.
    if (image != nil){
        // If image != nil, set cellImage to that image
        cell.cellImage.image = image;
    }
    [image release];
}

スクロールの遅れを避けるために、各セルの画像を「遅延ロード」する最良の方法は何ですか?

4

1 に答える 1

7

SDWebImageリポジトリを見てください。非同期の画像読み込みを実行するためのすべてを提供します。

アップデート

README にタイプミスがあることに気付きました。そのため、ローカル ファイルのダウンロードが期待どおりに機能しない可能性があります。

ここにいくつかのサンプルコードがあります。ビュー コントローラーには UIImageView アウトレットがあり、image.jpgファイルをロードしたいと考えています。SDWebImageManagerDelegate以下のプロトコルを実装します。

- (IBAction)loadImage:(id)sender {
    SDWebImageManager *manager = [SDWebImageManager sharedManager];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *destPath = [documentsDirectory stringByAppendingPathComponent:@"image.jpg"];
    NSURL *url = [NSURL fileURLWithPath:destPath];
    UIImage *cachedImage = [manager imageWithURL:url];
    if (cachedImage)
    {
        imageView.image = cachedImage;
    }
    else
    {
        // Start an async download
        [manager downloadWithURL:url delegate:self];
    }    
}

- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image
{
    imageView.image = image;
}
于 2010-06-10T08:54:04.483 に答える