Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.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 UICollectionviewCell图像加载问题_Ios_Objective C_Iphone_Xcode - Fatal编程技术网

Ios UICollectionviewCell图像加载问题

Ios UICollectionviewCell图像加载问题,ios,objective-c,iphone,xcode,Ios,Objective C,Iphone,Xcode,我正在尝试将UICollectionView与多个图像集成。我从API动态获取图像,并使用SDWebImage库将其直接加载到UICollectionViewCell中。这对我来说很好。但现在我试图将从文档目录下载的图像加载到UICollectionViewCell,它在错误的单元格中显示图像。每次我加载UICollectionViewCell或滚动它时,单元格中的图像都会发生更改 -(void)setImageFromDocumentDirWithName:(NSString *)strIma

我正在尝试将UICollectionView与多个图像集成。我从API动态获取图像,并使用SDWebImage库将其直接加载到UICollectionViewCell中。这对我来说很好。但现在我试图将从文档目录下载的图像加载到UICollectionViewCell,它在错误的单元格中显示图像。每次我加载UICollectionViewCell或滚动它时,单元格中的图像都会发生更改

-(void)setImageFromDocumentDirWithName:(NSString *)strImageName setImageToView:(UIImageView *)imgView imgURL:(NSURL *)url{

    NSString *workSpacePath=[[Utility applicationDocumentsDirectory] stringByAppendingPathComponent:strImageName];

    BOOL fileExists=[[NSFileManager defaultManager] fileExistsAtPath:workSpacePath];
    if (fileExists) {
        imgView.image=[UIImage imageWithData:[NSData dataWithContentsOfFile:workSpacePath]];
    }
    else{
        [imgView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Default1"]];
    }
}
当我直接从服务器加载图像时,它对我来说很好。但我想检查图像文件是否存在于我的文档目录中。如果是,则从文档目录加载,否则从服务器加载。 请提供您宝贵的建议

我在这里设置手机

    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                 cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell  *myCell = [collectionView                         dequeueReusableCellWithReuseIdentifier:@"MyCell"
                                    forIndexPath:indexPath];
    UILabel *recipeNameLabel = (UILabel *)[myCell viewWithTag:200];
    recipeNameLabel.text = [[arrAddSpace objectAtIndex:indexPath.row] objectForKey:@“name”];

    UIImageView *imgSpace = (UIImageView *)[myCell viewWithTag:100];

    NSString *strURL = [NSString stringWithFormat:@"%@",[[arrAddSpace objectAtIndex:indexPath.row] objectForKey:@"thumbnail_url"]];
    if ([strURL length]!=0 || ![strURL isEqualToString:@""]) {
       NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",strURL]];

        [self setImageFromDocumentDirWithName:[[arrAddSpace objectAtIndex:indexPath.row] objectForKey:@"thumbnail_url"] setImageToView:imgSpace imgURL:url];
    return myCell;
    }
}

这是一个与重复使用单元格有关的问题&同时将图像设置为不正确的单元格

在UICollectionView或UITableView中,为了实现最大性能并减少内存消耗,使用了可重用单元的概念。基本概念是集合视图和表视图最初只创建可见的单元格,当我们滚动时,已经创建的单元格将被重用,并需要使用相应索引路径(单元格)的内容更新单元格的子视图(容器)

在您创建集合视图时,单元格的图像将正确显示,直到您滚动为止。但是,一旦您滚动单元格,这些单元格就会被重新使用,并且打算在单元格上显示的图像不会显示在那里

要解决此问题,您需要根据索引路径的数据更新单元格的图像,以指向相同索引路径的单元格


此外,如果您需要了解代码中的确切问题,请张贴您创建单元格的源代码,并将图像设置到单元格中

您能做一件事吗,只需在方法中再添加一个参数“IndexPath”-

-(void)setImageFromDocumentDirWithName:(NSString *)strImageName setImageToView:(UIImageView *)imgView imgURL:(NSURL *)url
作为

并将其定义替换为-

-(void)setImageFromDocumentDirWithName:(NSString *)strImageName setImageToView:(UIImageView *)imgView imgURL:(NSURL *)url forIndexPath:(NSIndexPath *)indexPath{

UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];

if(cell ! =nil)
{
     UIImageView *imageView = [cell viewWithTag:100];

     NSString *workSpacePath=[[Utility applicationDocumentsDirectory] stringByAppendingPathComponent:strImageName];

    BOOL fileExists=[[NSFileManager defaultManager] fileExistsAtPath:workSpacePath];
    if (fileExists) {
        imageView.image =[UIImage imageWithData:[NSData dataWithContentsOfFile:workSpacePath]];
    }
    else{
        [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Default1"]];
    }
}
}

您有设置单元格内容的源代码吗?我有一个和你相似的问题。我不确定这是否是您的情况,但当您滚动tableview或collectionview时,iOS会尝试重用相同类型的单元格。因此,如果您已在现有单元格中加载了以前的图像,则要下载以在“新”单元格中显示的新图像在您下载之前不会显示。因此,旧的一个将一直保持到。UIImageView*imgSpace=(UIImageView*)[myCell viewWithTag:100];[self-setImageFromDocumentDirWithName:[[arrAddSpace objectAtIndex:indexPath.row]objectForKey:@“缩略图url”]setImageToView:imgSpace imgURL:url];我正在传递imgview,我希望将此图像作为参数显示在其上,以设置ImageFromDocumentDirWithName方法。是否有自定义UICollectionViewCell?因为这样会更容易。您可以在cellForItemAtIndexPath->(最好通过gist:)内部执行此操作。您的代码的问题在于,它没有在预期索引路径的单元格上设置imageView的图像,有关详细信息,请参阅以下答案。我已编辑了我的问题,请检查我设置单元格是否有误此始终返回单元格nil您是否也调用了传递索引路径的方法-[self-setImageFromDocumentDirWithName:[[arrAddSpace objectAtIndex:Indexath.row]objectForKey:@“缩略图url”]setImageToView:imgSpace imgURL:url ForIndexath:Indexath];
-(void)setImageFromDocumentDirWithName:(NSString *)strImageName setImageToView:(UIImageView *)imgView imgURL:(NSURL *)url forIndexPath:(NSIndexPath *)indexPath{

UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];

if(cell ! =nil)
{
     UIImageView *imageView = [cell viewWithTag:100];

     NSString *workSpacePath=[[Utility applicationDocumentsDirectory] stringByAppendingPathComponent:strImageName];

    BOOL fileExists=[[NSFileManager defaultManager] fileExistsAtPath:workSpacePath];
    if (fileExists) {
        imageView.image =[UIImage imageWithData:[NSData dataWithContentsOfFile:workSpacePath]];
    }
    else{
        [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Default1"]];
    }
}
}