【发布时间】:2011-09-29 16:52:34
【问题描述】:
StackOverflow 的朋友和同事程序员,
我的 RootViewController(视图上的花表视图)应该显示单元格的标题、副标题和图像缩略图(从相机胶卷加载)。我猜是一个非常基本的表格。
所有内容都存储在“核心数据”中,但图像存储为相机胶卷的 imagePaths。示例: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;
}
【问题讨论】:
标签: iphone ios image objective-c-blocks alasset