【发布时间】:2013-12-01 02:27:08
【问题描述】:
我有一个UICollectionView,它显示了来自 CoreData 实体的一些数据。这个实体有很多属性,其中之一就是image(可变形类型)。
我的UICollectionView 滚动不流畅。我认为原因是 image 属性中的大图片(从 300 到 500k)。
这是我cellForItemAtIndexPath的代码:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
AppDelegate *d = (AppDelegate *)[[UIApplication sharedApplication] delegate];
static NSString *CellIdentifier = @"cellRecipe";
RecipeCell *cell = (RecipeCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.image.layer.borderColor = [UIColor blackColor].CGColor;
cell.image.layer.borderWidth = 1.0f;
Recipes *recipe = [recipesArray_ objectAtIndex:indexPath.item];
if ([recipe.isPaid integerValue] == 0)
[cell.freeImage setHidden:NO];
else [cell.freeImage setHidden:YES];
NSObject *object = [[NSUserDefaults standardUserDefaults] objectForKey:@"com.________.fullhomechef"];
if ([object isEqual:[NSNumber numberWithBool:YES]]) {
[cell.freeImage setHidden:YES];
}
if ([recipe.isFavorite integerValue] == 0)
[cell.favImage setImage:[UIImage imageNamed:@"toFavorite.png"]];
else [cell.favImage setImage: [UIImage imageNamed:@"toFavorite_.png"]];
if ([recipe.isFitness integerValue] == 1)
[cell.fitImage setHidden:NO];
else [cell.fitImage setHidden:YES];
if ([d.currLang isEqualToString:@"ru"])
[cell.recipeName setText: recipe.nameru];
else [cell.recipeName setText: recipe.nameen];
[cell.image setImage:recipe.image];
int difficulty = [recipe.difficulty intValue];
[cell.difficultyImage setImage: [mainTabController imageForRating:difficulty]];
return cell;
}
我已尝试评论行[cell.image setImage:recipe.image];,但滚动仍在
间歇性的。
我做错了什么?可能有一些方法可以处理大图像吗?因为在以前的版本中我使用了大小为 50-70k 的图像。
附:如果我耐心地向下滚动到最后,滚动会变得更加流畅。
Guto Araujo 的回答之后
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cellRecipe";
RecipeCell *cell = (RecipeCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.image.layer.borderColor = [UIColor blackColor].CGColor;
cell.image.layer.borderWidth = 1.0f;
Recipes *recipe = [fetchRecipes objectAtIndexPath:indexPath];
if ([recipe.isPaid integerValue] == 0) {
[cell.freeImage setHidden:NO];
}
else [cell.freeImage setHidden:YES];
if (wasPaid == YES) {
[cell.freeImage setHidden:YES];
}
if ([recipe.isFavorite integerValue] == 0)
[cell.favImage setImage:[UIImage imageNamed:@"toFavorite.png"] ];
else [cell.favImage setImage: [UIImage imageNamed:@"toFavorite_.png"]];
if ([recipe.isFitness integerValue] == 1)
[cell.fitImage setHidden:NO];
else [cell.fitImage setHidden:YES];
if ([d.currLang isEqualToString:@"ru"]) {
[cell.recipeName setText: recipe.nameru];
} else [cell.recipeName setText:recipe.nameen];
__weak typeof(self) weakSelf = self;
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
UIImage *image = recipe.image;
dispatch_async(dispatch_get_main_queue(), ^{
// Set via the main queue if the cell is still visible
if ([weakSelf.collectionView.indexPathsForVisibleItems containsObject:indexPath]) {
RecipeCell *cell =
(RecipeCell *)[weakSelf.collectionView cellForItemAtIndexPath:indexPath];
cell.image.image = image;
}
});
}];
[thumbnailQueue addOperation:operation];
int difficulty = [recipe.difficulty intValue];
[cell.difficultyImage setImage: [mainTabController imageForRating:difficulty]];
return cell;
}
【问题讨论】:
-
为什么每次都分配一个新的
MainTabController? -
所以你将图片存储在数据库本身?如果是这样,请不要这样做;)只需在您的数据库中存储一个路径并将图像存储在一个文件夹中。
-
您可以尝试衡量性能;)这不是数据库的用途;)。您甚至应该能够创建一个文件夹结构,在其中保存带有路径和名称的图像,这样您就不需要路径属性(例如,如果您选择配方名称,您可以像
[UIImage imageNamed:[[NSString stringWithFormat:@"%@", recipe.name]]];一样动态加载它)抱歉任何语法错误,但我在移动设备上…… -
@HAS 非常感谢!我会试着给你写结果!
-
Romowski,很高兴听到它成功了! @HAS 这是一个很好的辩论:存储。据我了解,Core Data 实际上在选择该选项的情况下将图像存储在数据库之外。我想缺点是它是信仰的飞跃而不是直接管理它。无论如何,我认为这两点都是公平的。感谢您的精彩辩论!
标签: ios uiimageview uicollectionview uicollectionviewcell