Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 滚动UICollectionView时图像会更改_Ios_Objective C_Uicollectionview - Fatal编程技术网

Ios 滚动UICollectionView时图像会更改

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

Im使用UICollectionView加载图像

下面是我的代码。ImageArray将包含必须加载的每个图像的url。在我的代码中,我试图在整个collectionview周围给出一个圆形边框

- (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的每个单元格,但圆边框不会渲染到整个UICollectionView

  • 如何进行分类?

    您可以将边框和角半径设置为集合视图,如

      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
    。然后像你用电池一样把它围起来。