我有一个滚动图像的分页UICollectionView。每个图像都充满了整个屏幕。对于普通照片,我的collectionView可以流畅地滚动,但对于全景拍摄,当我滚动图像时,它会开始滞后。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
imageCell *cell = (imageCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.tag = indexPath.row;
PFObject *temp = [_dataArray objectAtIndex:indexPath.row];
PFUser *user = [temp objectForKey:@"user"];
PFFile *file = [temp objectForKey:@"image"];
[file getDataInBackgroundWithBlock:^(NSData *data, NSError *error){
if (!error) {
cell.selectedImageView.image = [UIImage imageWithData:data];
self.navigationItem.title = [user objectForKey:@"Name"];
}
}];
return cell;
}如你所见,我在后台加载了图像。
我是否需要在willDisplayCell中执行一些操作?谢谢
发布于 2015-07-09 14:21:45
您每次都在加载数据。尝试一些方法来防止这种情况的发生。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
imageCell *cell = (imageCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
...
if (cell.selectedImageView.image == nil)
{
[file getDataInBackgroundWithBlock:^(NSData *data, NSError *error){
if (!error) {
cell.selectedImageView.image = [UIImage imageWithData:data];
self.navigationItem.title = [user objectForKey:@"Name"];
}
}];
}
return cell;
}此外,有时它只是在模拟器中。尝试重置模拟器或Xcode,然后再次运行。
我有经验,有时,我甚至检查可能的内存处理错误,但重启模拟器做了工作。
发布于 2015-07-09 14:39:09
我以前从来没有真正使用过Parse对象,但似乎是因为一个cell试图同时加载多个图像。我认为您应该将加载图像的逻辑移动到单元格中,并在prepareForReuse中重用图像时取消图像加载。当您在单元格中加载图像时,通常会使用此方法。我将给你一个快速的例子,希望它能给你一个想法。
在imageCell中,
var file: PFFile? {
didSet {
if let f = file {
[file getDataInBackgroundWithBlock:^(NSData *data, NSError *error){
if (!error) {
selectedImageView.image = [UIImage imageWithData:data];
}
}];
}
}
}
........
override func prepareForReuse() {
super.prepareForReuse()
if let f = file {
f.cancel()
}
}在collectionView中: cellForItemAtIndexPath:
.....
cell.file = file
.....https://stackoverflow.com/questions/31309474
复制相似问题