Ios 滚动UICollectionView时图像会更改
Im使用UICollectionView加载图像 下面是我的代码。ImageArray将包含必须加载的每个图像的url。在我的代码中,我试图在整个collectionview周围给出一个圆形边框Ios 滚动UICollectionView时图像会更改,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,Im使用UICollectionView加载图像 下面是我的代码。ImageArray将包含必须加载的每个图像的url。在我的代码中,我试图在整个collectionview周围给出一个圆形边框 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString *ident
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
CALayer* layer = cell.layer;
[layer setCornerRadius:4.0f];
[layer setBorderColor:[UIColor colorWithWhite:0.8 alpha:1].CGColor];
[layer setBorderWidth:1.0f];
if ([ImageArray count] >0){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
NSData *data0 = [NSData dataWithContentsOfURL: [NSURL URLWithString:[ImageArray objectAtIndex:indexPath.row]]];
UIImage *image = [UIImage imageWithData: data0];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
recipeImageView.image = image;
});
});
}
[spinnerShow stopAnimating];
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
return cell;
}
我面临的问题是
如何进行分类?您可以将边框和角半径设置为集合视图,如
collectionView.layer.borderWidth = 1.0;
collectionView.layer.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1].CGColor;
collectionView.layer.cornerRadius = 4.0f;
并且不要在cellForItemAtIndexPath
中设置它。在viewdidload
中设置它,以便它加载一次,否则每次单元格变形时它都会设置
第二件事,正如一条评论中所建议的,您应该使用它来缓存图像
如果使用自动布局,请确保设置了正确的约束
希望这会有所帮助:)您可以将边框和角半径设置为集合视图,如
collectionView.layer.borderWidth = 1.0;
collectionView.layer.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1].CGColor;
collectionView.layer.cornerRadius = 4.0f;
并且不要在cellForItemAtIndexPath
中设置它。在viewdidload
中设置它,以便它加载一次,否则每次单元格变形时它都会设置
第二件事,正如一条评论中所建议的,您应该使用它来缓存图像
如果使用自动布局,请确保设置了正确的约束
希望这会有所帮助:)您正在引用单元格的标记,但所有单元格的标记值均为100。这意味着您将得到不可预测的结果,这就是图像不断变化的原因。您正在引用单元格的标记,但所有单元格的标记值均为100。这意味着您将得到不可预测的结果,这就是图像不断更改的原因。
UICollectionView
将重用现有单元格。因此,您的collectionViewCell
中的imageView
可能会显示先前加载的图像
这不是一个完美的解决方案,但在开始下载之前,请尝试将nil
或默认图像传递到imageView
recipeImageView.image = nil;
以前
if ([ImageArray count] > 0) {
UICollectionView
将重用现有单元格。因此,您的collectionViewCell
中的imageView
可能会显示先前加载的图像
这不是一个完美的解决方案,但在开始下载之前,请尝试将nil
或默认图像传递到imageView
recipeImageView.image = nil;
以前
if ([ImageArray count] > 0) {
在
UICollectionViewCell
中,是一种方法prepareforeuse
,您应该覆盖它并将UIImageView
的图像设置为nil
在UICollectionViewCell
中,是一种方法prepareforeuse
,您应该覆盖它并将UIImageView
的图像设置为nil
使用SDWebImage,对于第二个问题,您将单元格的层指定给层
,这就是为什么边框没有呈现给整个UICollectionView。如何才能将其显示给整个UICollectionView?感谢您不熟悉Obj-c,我认为您应该这样做:CALayer*layer=collectionView.layer
。然后像处理单元格一样将其四舍五入。使用SDWebImage,对于第二个问题,您可以将单元格的层指定给层
,这就是为什么边框没有呈现给整个UICollectionView。如何才能将其显示给整个UICollectionView?感谢您不熟悉Obj-c,我认为您应该这样做:CALayer*layer=collectionView.layer
。然后像你用电池一样把它围起来。