[英]Buggy UICollectionView when loading cached image from parse
我有一些解析图像。 (每个对象有5个PFFile图像,每个图像约800x800 400Kb)。 当我第一次在设备(iphone 5S)上运行该应用程序时,将加载图像,并且一切正常。 当我向下滚动以加载全部。 然后我再次聚拢,图像更早地加载(至少一次并通过解析缓存),UICollectionView闪烁了一点(不是太慢但是很明显,就像UI试图跟上拖动一样)。 我有什么我想念的吗?
*编辑1:非常感谢我的代码的任何帮助或完善
调用CustomCell:
static NSString *cellIdentifier = @"cellImage";
cellTypeImage1 *cell = (cellTypeImage1 *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
[cell configureWithObject:[self.collectionView objectAtIndex:indexPath]];
return cell;
在自定义单元内
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.fullResImage = [[UIImageView alloc] init];
self.fullResImage.frame = CGRectZero;
[self addSubview:self.fullResImage];
}
self.backgroundColor = [color whiteColor];
return self;
}
-(void)layoutSubviews{
[super layoutSubviews];
self.fullResImage.frame = CGRectMake(0, 50, self.frame.size.width, self.frame.size.width);
}
- (void)prepareForReuse {
[super prepareForReuse];
self.fullResImage.image = nil;
}
-(void)configureWithObject:(PFObject*)object{
[object[@"image"] getDataInBackgroundWithBlock:^(NSData *data, NSError*error){
if (!error) {
/*THE PROBLEM IS HERE*/
self.fullResImage.image = [UIImage imageWithData:data];
/*When i replace it by a local image [UIImage imageNamed:@"LARGE IMAGE ABOUT 1.2MB"] everything runs smoothly*/
}else{
[ParseErrorHandlingController handleParseError:error];
}}];
}
尝试使用PFImageView而不是UIImageView。
if (object) {
cell.image.file = [object objectForKey:@"image"];
if ([cell.image.file isDataAvailable]) {
[cell.image loadInBackground];
}
}
在单元格中下载图像时,请记住在prepareForReuse中取消当前下载,否则快速滚动时会看到一些重复项。
我相信PFImageView会为您处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.