的iOS示出从ALAsset中的UITableView图像(iOS showing Image fr

2019-08-02 12:25发布

我有项目的存储位置分贝。 这些项目可以是不同类型的,如文本,视频和图像类型。 当查看加载我取从DB这些项目,我告诉他们在一个UITableView。 我现在面临的问题是,以显示表视图图像相关。 基本上在DB I存储与图像的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: 性能似乎更好,但它需要更多的时间来获取图像。

任何帮助将非常感激。

谢谢!

Answer 1:

有迹象表明,在这里可以提高几件事情。

第一个是,你想通了:做资产的装载在后台线程,然后将图像添加到细胞,当它准备在主线程上。 接下来,您要创建的对象ALAssetsLibrary为您展示的每一个细胞。 理想情况下,应用程序应该只有一个对象ALAssetsLibrary您保留,只要你需要它。 创建ALAssetsLibrary第一次你需要,然后再用它。

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

而最后,您使用的是fullResolutionImage中的tableview细胞。 如果你真的只需要展示形象,一个thumbnailImage或至少fullScreenImage就足够好了。

- (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 showing Image from ALAsset in UITableView