问题描述
-
以下是该问题的视频:
尝试从资产库中为UITableView设置[cell imageView]的图像时遇到问题。
I have a problem when trying to set the image for the [cell imageView] in a UITableView from the asset library.
图像已加载但在触摸(选择)该单元格之前它不会出现在单元格中。这是我的代码设置单元格的imageView:
The image is loaded but it doesn't appear in the cell until I touch (select) that cell. Here is my code that sets the imageView of the cell:
//Start loading the image using the image path
NSString *path = [occasion imagePath];
if (path != nil && [path hasPrefix:@"ass"])
{
NSLog(@"photo loaded from assets library");
//testing ALAssestLibrary
ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init];
ALAssetsLibraryAssetForURLResultBlock resultsBlock = ^(ALAsset *asset)
{
ALAssetRepresentation *representation = [asset defaultRepresentation];
CGImageRef imageRef = [representation fullResolutionImage];
UIImage *image = [[UIImage alloc] initWithCGImage:imageRef];
[[cell imageView] setImage:[image imageByScalingAndCroppingForSize:CGSizeMake(36.0, 42.0)]];
[image release];
};
ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error){
NSLog(@"FAILED! due to error in domain %@ with error code %d", error.domain, error.code);
// This sample will abort since a shipping product MUST do something besides logging a
// message. A real app needs to inform the user appropriately.
abort();
};
//Convert path to an NSUrl
NSURL *url = [NSURL URLWithString:path];
// Get the asset for the asset URL to create a screen image.
[assetsLibrary assetForURL:url resultBlock:resultsBlock failureBlock:failureBlock];
// Release the assets library now that we are done with it.
[assetsLibrary release];
}
以上代码属于:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
任何想法?
推荐答案
我想在这你正在使用iOS定义的UITableViewCell(而不是自定义的),这是你从initWithStyle:reuseIdentifier:获得的那些单元格之一。现在这些单元格内部构建的方式是,如果您不立即分配某些内容,则不会在单元格的contentView层次结构中添加相应的子视图。这种行为是复杂自定义视图的典型行为,其中最终单元格布局只有在其所有子视图都完全指定其内容时才能确定。 (例如,你有两个标签,比如标签A和标签B,标签A是多行的,你想在标签A的正下方添加标签B,那么只有在你有标签-A内容后才能正确放置标签-B并计算它的高度。所以我认为内置的iOS表格单元格以相同的方式完成,子视图层次结构是在layoutSubviews阶段构建的。
I think in this case you're using a iOS defined UITableViewCell (and not a custom one), that is one of those cells that you get from "initWithStyle:reuseIdentifier:". Now these cells are internally built in such way that if you don't assign some content immediately the corresponding subview will not be added in the cell's contentView hierarchy. This kind of behavior is typical of complex custom views, where the final cell layout can be determined only when all its subviews have their content fully specified. (E.g. you have two labels, let's say label A and label B, label A is multirow and you want to add label-B immediately below label-A, then you can place label-B correctly only once you have the label-A content and you calculate its height.). So I suppose the built-in iOS table cells are done in the same way and the subviews hierarchy is built at the "layoutSubviews" stage.
在您的情况下,您可以创建单元格但延迟提供图像的时间。但是此时layoutSubviews已经被调用,没有任何图像,相应的imageView甚至没有被分配并添加到contentView层次结构中!
In your case you create the cell but defer the time the image is provided. But at this point the layoutSubviews has been called yet and without any image the corresponding imageView is not even allocated and added in the contentView hierarchy!
所以,据我说,您的解决方案就是立即为您的资产分配一个占位符(占位符当然可以是透明图像),这将保证您创建内部UIImageView。小心图像大小,它应该接近预期的图像大小,原因与上面解释的相同。一旦你的完成块被调用,图像视图应该已经存在并且图像将会出现。
So, according to me, the solution for you is just to assign immediately a "placeholder" for your asset (the placeholder can be a transparent image of course) which will guarantee you of the internal UIImageView creation. Be careful with the image size, it should be close to the expected image size, for the same reason explained above. As soon as your finish block is called the image view should be already there and the image will appear.
当您点击图像出现的单元格时,原因是由于此操作再次调用layoutSubviews。在这种情况下,整个代码可能会重新执行,因为您之前已经调用了setImage:,然后设置内部image属性,并使用新图像重建contentView层次结构。
The reason why when you tap on the cell the image appears, is due to the fact that this operation calls the layoutSubviews again. In this case the whole code is probably re-executed and as you already called "setImage:" before then the internal "image" property is set and the contentView hierarchy will be rebuilt with the new image.
您的问题的另一个可能的解决方案当然是使用自定义UITableViewCell(例如从Nib加载)。在这种情况下,所有子视图都将被加载,您只需使用他的标签访问UIImageView(阅读苹果文档以了解更多有关从Nib文件创建表视图单元的有用技术)。
Another possible solution to your problem is of course to use a "custom" UITableViewCell (e.g. loaded from a Nib). In such case all your subviews will be loaded and you will simply access to the UIImageView using his tag (read apple docs to know more about this useful technique to create table view cells from a Nib file).
这篇关于UITableView中的cell imageView在选中之前不会出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!