Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone RootViewController-tableView:cellForRowAtIndexPath-从资产库加载映像_Iphone_Ios_Image_Objective C Blocks_Alasset - Fatal编程技术网

Iphone RootViewController-tableView:cellForRowAtIndexPath-从资产库加载映像

Iphone RootViewController-tableView:cellForRowAtIndexPath-从资产库加载映像,iphone,ios,image,objective-c-blocks,alasset,Iphone,Ios,Image,Objective C Blocks,Alasset,我的朋友和同事程序员 我的RootViewController(视图上的flowerTableView)应该显示单元格的标题、副标题和图像缩略图(从相机卷加载)。我想这是一张非常基本的桌子 所有内容都存储在“核心数据”中,但图像作为图像路径存储到相机卷。示例:flower.imagePath=assets-library://asset/asset.JPG?id=1000000002&ext 使用底部的代码,一切都应该顺利运行,但事实并非如此。启动应用程序时,会显示标题和副标题,但不会显示图像。

我的朋友和同事程序员

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

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

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

当我按下“全部显示”时,工具栏上的一个按钮将执行以下代码

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];
然后重新载入表格,所有美丽的花朵现在都显示出来了

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

调试此代码时,字符串“此调试字符串是在完成此功能后记录的”是在启动应用程序时加载表后记录的,而不是在按“全部显示”后记录的 这意味着,所有图像都已成功从摄影机滚轮加载,但在显示单元后已连接到单元,因此未显示

按“全部显示”时,将按预期打印同一行

希望有人能告诉我这里发生了什么,甚至更好的是,我的代码中应该做些什么改变才能让它工作。我现在陷入困境

谢谢你帮助我! 埃德温

::代码:::

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

-[AlassetLibrary assetForURL:resultBlock:failureBlock]异步运行。这意味着调用会立即在tableView:cellforrowatinexpath:method中返回。在实际加载资源之前,单元格将显示在表视图中

您需要做的是为结果块中的单元格设置图像。大概是这样的:

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];
}

让我提出以下解决方案:

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

创建一个NSMutableArray,其中填充了要在表中显示的UITableViewCells(与您在CellForRowatineXpath方法中创建的相同) 只需在CellForRowatineXpath方法中返回相关的UITableViewCell(它将是NSMutableArray中的一个对象)


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

此外,请注意,不同版本的iOS之间,资源URL的格式可能会发生变化。您正在将资产URL存储在数据库中,但不能保证当用户升级到iOS5时它们仍能工作。我也尝试了这一行代码来完成此任务,但由于异步执行,它也失败了。图像已加载但未显示。我需要的是在表之后重新加载数据:cellforrowatinexpath,但这将永远循环…;-)还有一件事,我听起来有点像科伦坡。。。如果图像加载是在异步事件中完成的,那么为什么当我按下“全部显示”时,会以完全相同的方式执行完全相同的功能,但现在图像加载是在同步事件中进行的呢。因此,当我进行一些调试时,第一个输出是:
-function start,-function end,-load image
。第二个输出是:
-function start,-loading image,-function end
。关键是该方法可能会异步运行,因此您必须假设它会。不可能知道AlassetLibrary在内部做什么以及何时调用完成块。例如,在第一次调用时,ALAssetLibrary需要检查用户是否已授予您的应用程序访问库的权限,如果没有,可能会显示一个对话框。它还可以缓存资产,并在可能的情况下同步返回。感谢您的帮助!我将添加一些代码来存储核心数据中的图像缩略图,这些缩略图将显示在表中。不再为表视图执行异步代码…在CellForRowatineXpath委托方法中创建单元格的意义在于,您可以利用UITableView对单元格的重用和取消查询。这意味着您只需要创建最少数量的单元格,即使您的表格包含数千行。感谢您的反馈和大脑循环。你能给我一些你将如何实现这个的代码吗?请记住,可能会使用1000条带有图像的记录。不确定您在回复谁,Edwin、me或Zigglzworth,但不要这样做。UITableView的一个关键特性是它将重用单元实例。如果你接受了Zigglzworth的建议,你就绕过了这个功能。改为解决原来的问题。@Robin:显然,当uitableview向下滚动时,您可以将更多单元格加载到数组中,而不需要一次向数组中添加1000个单元格。我已经使用过很多次了,它工作得非常出色。@Zigglzworth:好的,但是如果你正确使用它,你基本上是在重新实现UITableView提供的一个单元缓存。