【问题标题】:Buggy UICollectionView when loading cached image from parse从解析加载缓存图像时出现错误的 UICollectionView
【发布时间】:2015-11-22 09:26:24
【问题描述】:

我有一些正在解析的图像。 (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];
        }}];

}

【问题讨论】:

    标签: ios objective-c xcode parse-platform uicollectionview


    【解决方案1】:

    尝试使用 PFImageView 而不是 UIImageView。

    if (object) {
         cell.image.file = [object objectForKey:@"image"];
         if ([cell.image.file isDataAvailable]) {
             [cell.image loadInBackground];
         }
    }
    

    在单元格内下载图片时,记得在 prepareForReuse 中取消当前下载,否则在快速滚动时会看到一些重复。

    我相信 PFImageView 会为你处理这个问题。

    【讨论】:

    • 谢谢你成功了。我没有使用 PFImageView 因为 Xcode 无法识别它。但是,当您提出建议时,我四处寻找,发现我必须包含 #import 。现在一切正常
    猜你喜欢
    • 1970-01-01
    • 2016-12-27
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    相关资源
    最近更新 更多