0

アイテムが保存されているデータベースがあります。これらのアイテムは、テキスト、ビデオ、画像など、さまざまな種類のアイテムにすることができます。ビューが読み込まれると、DB からこれらのアイテムを取得し、UITableView に表示します。私が直面している問題は、テーブル ビューに画像を表示することに関連しています。基本的に、DB に画像に関連する ALAsset リンクを保存し、そこから次のコードを使用して画像を取得しようとします。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

 //...Other Code

  else if ([object isMemberOfClass:[Picture class]]){

            //Get a reusable cell
            cell = [tableView dequeueReusableCellWithIdentifier:@"pictureCellIdentifier"];

            // [self performSelectorInBackground:@selector(performAsset:) withObject:dict];

            ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
            {
                ALAssetRepresentation *rep = [myasset defaultRepresentation];
                CGImageRef iref = [rep fullResolutionImage];
                if (iref) {
                    ((PictureViewCell *)cell).placeHolderImageView.hidden = YES;
                    ((PictureViewCell *)cell).pictureImageView.image =  [UIImage imageWithCGImage:[rep fullResolutionImage]  scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];

                }
            };


            ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
            {
                 [Utility showAlertViewWithTitle:@"Location Error" message:@"You must activate Location Services to access the photo" cancelButtonTitle:@"Dismiss"];

            };

            ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
            [assetslibrary assetForURL:[NSURL URLWithString:((Picture *)objectInArray).imagePath]
                           resultBlock:resultblock
                          failureBlock:failureblock];

         //Set the background for the cell
            cell.backgroundView = iv;

        }

 //...Other code

}

問題は、セルをスライドするとメソッドが呼び出され、アプリが非常に遅くなることです。だから、私がやろうとしていることを達成するためのより良い方法があると思います. また、 を使用してそのコードを実行しようとしましたperformselectorInBackground:。パフォーマンスは向上しているように見えますが、画像の取得に時間がかかります。

どんな助けでも本当に感謝しています。

ありがとう!

4

1 に答える 1

0

ここで改善できることがいくつかあります。

1 つ目は、ご理解のとおりです。バックグラウンド スレッドでアセットの読み込みを行い、メイン スレッドで準備ができたらセルに画像を追加します。ALAssetsLibrary次に、表示するすべてのセルのオブジェクトを作成しています。理想的には、アプリには、ALAssetsLibrary必要な限り保持するオブジェクトが 1 つだけある必要があります。ALAssetsLibrary必要なときに初めて作成し、再利用します。

- (ALAssetsLibrary *)defaultAssetsLibrary {
    if (_library == nil) {
        _library = [[ALAssetsLibrary alloc] init];
    }
    return _library;
}

最後にfullResolutionImage、テーブルビュー セルで を使用しています。本当に画像を表示する必要がある場合は、 athumbnailImageまたは少なくとも afullScreenImageで十分です。

- (void) loadImage:(NSNumber *)indexPath url:(NSURL*)url
{
    int index = [indexPath intValue];

    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        CGImageRef iref = [[myasset defaultRepresentation] fullScreenImage];
        if (iref) {
            // TODO: Create a dictionary with UIImage and cell indexPath

            // Show the image on main thread
            [self performSelectorOnMainThread:@selector(imageReady:) withObject:result waitUntilDone:NO];
        }
    };
    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
    {
         [Utility showAlertViewWithTitle:@"Location Error" message:@"You must activate Location Services to access the photo" cancelButtonTitle:@"Dismiss"];

    };

    [[self defaultAssetsLibrary] assetForURL:url
                   resultBlock:resultblock
                  failureBlock:failureblock];
}

-(void) imageReady:(NSDictionary *) result
{
    // Get the cell using index path

    // Show the image in the cell
}
于 2012-11-20T14:43:13.477 に答える