Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.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:集合视图单元格中的重叠图像_Ios_Uiimageview_Uicollectionview - Fatal编程技术网

ios:集合视图单元格中的重叠图像

ios:集合视图单元格中的重叠图像,ios,uiimageview,uicollectionview,Ios,Uiimageview,Uicollectionview,我以编程方式创建了collection视图,其中我在collectionViewCell中以编程方式创建了UIImageview,并且imageview中显示的图像是从服务器异步下载的 下面的代码是用cellForItemAtIndexPath:方法编写的- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ NSURL *imageURL = [NSURL URLWith

我以编程方式创建了collection视图,其中我在collectionViewCell中以编程方式创建了UIImageview,并且imageview中显示的图像是从服务器异步下载的

下面的代码是用cellForItemAtIndexPath:方法编写的-

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    NSURL *imageURL = [NSURL URLWithString:[arrmImgPaths objectAtIndex:indexPath.row]];
    NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
    UIImage *image = [UIImage imageWithData:imageData];

   // [imgvMainView removeFromSuperview];
    // Now the image will have been loaded and decoded and is ready to rock for the main thread
    dispatch_sync(dispatch_get_main_queue(), ^{

        imgvMainView =[[UIImageView alloc] initWithFrame:CGRectMake(34,41,177,124)];
        [cell addSubview:imgvMainView];

        [imgvMainView setImage:image];
        imgvMainView.contentMode = UIViewContentModeScaleAspectFit;

    });
});
问题是,当我滚动集合视图时,一些图像彼此重叠。请告诉我避免它的方法。
提前感谢。

这种情况很可能发生,因为您正确地重用了单元格,并且以前使用过的单元格仍在调用以前的图像。当单元格消失时,必须取消异步任务。顺便说一句,使用AFNetworking+UIImageView进行此操作将使您的生活更加轻松

另一个选项是在创建UIimageView之前检查单元格中是否没有UIimageView

dispatch_sync(dispatch_get_main_queue(), ^{
    UIView *viewToRemove;
    for (UIView *view in cell.subViews) {
         if (view isKingOfClass:UIImageView){
             viewToRemove = view;
         }
    }

    [viewToRemove removeFromSuperView];

    imgvMainView =[[UIImageView alloc] initWithFrame:CGRectMake(34,41,177,124)];
    [cell addSubview:imgvMainView];

    [imgvMainView setImage:image];
    imgvMainView.contentMode = UIViewContentModeScaleAspectFit;

});

请使用下面的行,我希望这将工作的问题,我假设重复细胞

UIImageView*imgvMainView=[[UIImageView alloc]initWithFrame:CGRectMake34,41177124];
[cell.contentView addSubview:imgvMainView]

你能给截图添加一个链接吗?或者说它们重叠时更具描述性。我认为这是由于我在同步块中创建的图像视图造成的。但是,如果我停止异步任务,则我的集合视图将不会正确滚动。这并不意味着您需要在单元格出现时挂起图像请求。如果不是,原始请求仍然会发生,新请求也会发生,您将创建两个图像视图。请参阅我为您添加的另一个选项。我写的代码自由的风格,所以请纠正打字错误。