0

我有一个存储项目的数据库。这些项目可以是不同类型的类型,如文本、视频和图像。加载视图时,我从数据库中获取这些项目并将它们显示在 UITableView 中。我面临的问题与在表格视图中显示图像有关。基本上在数据库中,我存储与图片相关的 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

这里有一些可以改进的地方。

第一个如您所想:在后台线程中加载资产,然后在主线程上准备好图像时将其添加到单元格中。ALAssetsLibrary接下来,您将为您显示的每个单元格创建一个对象。ALAssetsLibrary理想情况下,只要您需要,应用程序应该只保留一个对象。ALAssetsLibrary在第一次需要时创建一个,然后重复使用它。

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

最后,您fullResolutionImage在 tableview 单元格中使用 。如果你真的只需要显示图像,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 回答