首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RootViewController - tableView:cellForRowAtIndexPath -从assetsLibrary加载图像

RootViewController - tableView:cellForRowAtIndexPath -从assetsLibrary加载图像
EN

Stack Overflow用户
提问于 2011-09-30 00:52:35
回答 2查看 1.6K关注 0票数 0

StackOverflow的朋友和同事程序员们,

我的RootViewController (视图上的flowerTableView )应该显示单元格的标题、副标题和图像缩略图(从相机胶卷加载)。我猜是一张非常基本的桌子。

所有内容都存储在“核心数据”中,但图像以imagePaths格式存储到相机胶卷中。示例:flower.imagePath = assets-library://asset/asset.JPG?id=1000000002&ext

使用底部的代码,一切都应该顺利运行,但事实并非如此。启动应用程序时,会显示标题和副标题,但不会显示图像。当我按下一个单元格,它显示的是详细视图,并再次弹回主视图时,将显示此特定单元格的图像。

当我按下“Show All”时,工具栏上的一个按钮将执行以下代码

代码语言:javascript
复制
NSFetchRequest *fetchRequest = [[self fetchedResultsController] fetchRequest];
[fetchRequest setPredicate:nil];

NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}
[self.flowerTableView reloadData];

然后重新装入表格,所有美丽的花朵现在都显示出来了。

为什么第一次没有展示这些花呢?这是缓存问题吗?

在调试此代码时,在启动应用程序时加载表之后,而不是在按下“Show all”之后,会记录字符串:“this debug string to after This function to”。这意味着所有图像都已成功从相机胶卷加载,但在显示单元后附加到单元,因此未显示。

当按下'Show All‘时,同一行被打印出来

希望有人能告诉我这里发生了什么,更好的是,在我的代码中要做哪些更改才能使其正常工作。我现在卡住了.

谢谢你帮了我一把!埃德温

代码:

代码语言:javascript
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *)indexPath
{

    Flower *flower = [fetchedResultsController_ objectAtIndexPath:indexPath];

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...

    // Display the image if one is defined
    if (flower.imagePath && ![flower.imagePath isEqualToString:@""])
    {
        // Should hold image after executing the resultBlock
       __block UIImage *image = nil;

        ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset *asset)
        {
            NSLog(@"This debug string was logged after this function was done");
            image = [[UIImage imageWithCGImage:[asset thumbnail]] retain];
        };

        ALAssetsLibraryAccessFailureBlock failureBlock  = ^(NSError *error)
        {
            NSLog(@"Unresolved error: %@, %@", error, [error localizedDescription]);
        };

        [assetsLibrary_ assetForURL:[NSURL URLWithString:flower.imagePath] 
                        resultBlock:resultBlock
                       failureBlock:failureBlock];

        [cell.imageView setImage:image];
    }

    return cell;
}
EN

回答 2

Stack Overflow用户

发布于 2011-09-30 02:08:11

-ALAssetsLibrary assetForURL:resultBlock:failureBlock异步运行。这意味着调用将在tableView:cellForRowAtIndexPath:方法中立即返回。在实际加载资产之前,单元格将显示在表视图中。

您需要做的是为结果块中的单元格设置图像。如下所示:

代码语言:javascript
复制
if (flower.imagePath && ![flower.imagePath isEqualToString:@""])
{
    ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset *asset)
    {
        NSLog(@"This debug string was logged after this function was done");
        [cell.imageView setImage:[UIImage imageWithCGImage:[asset thumbnail]]];

        //this line is needed to display the image when it is loaded asynchronously, otherwise image will not be shown as stated in comments
        [cell setNeedsLayout]; 

    };

    ALAssetsLibraryAccessFailureBlock failureBlock  = ^(NSError *error)
    {
        NSLog(@"Unresolved error: %@, %@", error, [error localizedDescription]);
    };

    [assetsLibrary_ assetForURL:[NSURL URLWithString:flower.imagePath] 
                    resultBlock:resultBlock
                   failureBlock:failureBlock];
}
票数 2
EN

Stack Overflow用户

发布于 2011-09-30 03:11:32

另一位Overflowist专家让我提出以下解决方案:

而不是在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *)indexPath中创建单元

创建一个用UITableViewCells填充的NSMutableArray (与您在cellForRowAtIndexPath方法中创建的相同),并简单地在cellForRowAtIndexPath方法中返回相关的UITableViewCell (它将是NSMutableArray中的对象)。

这样,cellForRowAtIndexPath方法将显示已经加载并准备显示的单元格。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7600693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档