簡體   English   中英

iOS在UITableView中顯示來自ALAsset的圖像

[英]iOS showing Image from ALAsset in UITableView

我有一個存儲項目的數據庫。 這些項目可以是不同類型的類型,例如文本,視頻和圖像。 加載視圖時,我從數據庫中獲取了這些項目,並在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:來執行該代碼。 性能似乎更好,但需要花費更多時間來獲取圖像。

任何幫助將不勝感激。

謝謝!

這里有些事情可以改進。

第一個是您所發現的:在后台線程中加載資產,然后在主線程上准備就緒時將圖像添加到單元中。 接下來,為顯示的每個單元格創建一個ALAssetsLibrary對象。 理想情況下,一個應用程序應該只保留一個ALAssetsLibrary對象,只要您需要它就可以保留它。 第一次需要時創建ALAssetsLibrary ,然后再使用它。

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

最后,您正在tableview單元格中使用fullResolutionImage 如果您真的只需要顯示圖像,那么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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM