iOS在UITableView中显示来自ALAsset的图像 [英] iOS showing Image from ALAsset in UITableView

查看:157
本文介绍了iOS在UITableView中显示来自ALAsset的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储项目的数据库。这些项目可以是不同类型的类型,如文本,视频和图像。加载视图时,我从数据库中获取这些项目,然后在UITableView中显示它们。我面临的问题与在表格视图中显示图像有关。基本上在DB中我存储与图片相关的ALAsset链接,并从中我尝试使用此代码获取其图像:

I have a db where items are stored. These items can be different kinds of type like text, videos and images. When the View is loaded I fetch these items from the DB and I show them in a UITableView. The problem I am facing is related to show the images in the table view. Basically in the DB I store the ALAsset link related to the picture and from it I try to get its image using this code:

- (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:执行该代码。性能似乎更好,但需要更多时间来获取图像。

The problem is that when you slide through the cell the method gets called and the app is really slow. So I guess there are better ways to achieve what I'm trying to do. I also tried to perform that code using performselectorInBackground:. Performance seem better but It takes more time to fetch the image.

任何帮助都会非常感激。

Any helps would be really appreciated.

谢谢!

推荐答案

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

There are a few things that can be improved here.

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

The first is as you figured out: Do the loading of the assets in background thread and then add the image to the cell when it is ready on the main thread. Next, you are creating an object of ALAssetsLibrary for every cell that you show. Ideally, an app should have just one object of ALAssetsLibrary that you retain as long as you need it. Create a ALAssetsLibrary the first time you need and then reuse it.

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

最后一个,你使用的是 fullResolutionImage < tableview单元格中的/ code>。如果你真的只需要显示图像, thumbnailImage 或至少一个 fullScreenImage 应该足够好。

And the last, you are using the fullResolutionImage in a tableview cell. If you really just need to display the image, a thumbnailImage or at least a fullScreenImage should be good enough.

- (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
}

这篇关于iOS在UITableView中显示来自ALAsset的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆