Ios 从parse下载图像并在UICollection视图中显示

Ios 从parse下载图像并在UICollection视图中显示,ios,objective-c,parse-platform,uicollectionview,Ios,Objective C,Parse Platform,Uicollectionview,我正在从parse下载图像,然后在uicollection视图中显示这些图像,但当我滚动collection视图时,它会挂起。这是我的密码 -(void)viewWillAppear:(BOOL)animated { CGRect screen = [[UIScreen mainScreen] bounds]; CGFloat height = CGRectGetHeight(screen); PFQuery * query = [PFQuery queryWithC

我正在从parse下载图像,然后在uicollection视图中显示这些图像,但当我滚动collection视图时,它会挂起。这是我的密码

-(void)viewWillAppear:(BOOL)animated
{
    CGRect screen = [[UIScreen mainScreen] bounds];

    CGFloat height = CGRectGetHeight(screen);

    PFQuery * query = [PFQuery queryWithClassName:@"Static_Wallpaper"];
    [query selectKeys:@[@"thumbnail"]];
    if (height == 480) {
        [query selectKeys:@[@"image4s"]];
    }
    else{
        [query selectKeys:@[@"image6s"]];
    }

    [query findObjectsInBackgroundWithBlock:^(NSArray * objects, NSError * error){
        if (!error) {
            [imgArry arrayByAddingObjectsFromArray:objects];
            imgArry = [[NSMutableArray alloc] initWithArray:objects];
            NSLog(@"%lu",(unsigned long)imgArry.count);
            [cllectionView reloadData];

        }

    }];

}
下面是在集合视图中填充图像的代码

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return imgArry.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";

    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    PFObject *imageObject = [imgArry objectAtIndex:indexPath.row];
    PFFile *imageFile = [imageObject objectForKey:@"image4s"];
    [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if (!error){
            NSLog(@"is there any data? %@", data);
            cell.imgView.image = [UIImage imageWithData:data];
            ;

        }
        else {
            NSLog(@"no data!");
        }
    }];



    return cell;
}

下载任务可能会阻止主线程。您可以尝试使用SDWebImage或异步下载任务并显示图像。您的下载任务可能会阻止主线程。您可以尝试使用SDWebImage或异步下载任务并显示图像,方法是添加以下代码:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";

    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    PFObject *imageObject = [imgArry objectAtIndex:indexPath.row];
    PFFile *imageFile = [imageObject objectForKey:@"image4s"];
    NSString *UserProfile = [file url];

                    if([[SDWebImageManager sharedManager] diskImageExistsForURL:[NSURL URLWithString:UserProfile]])
                    {
                        NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:[NSURL URLWithString:UserProfile]];
                        UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key];
                        cell.imgView.image=image;
                    }
                    else
                    {
                        cell.imgView.image=[UIImage imageNamed:@"UserUpdationImageCell.png"];
                        [cell.imgView  sd_setImageWithURL:[NSURL URLWithString:UserProfile] placeholderImage:[UIImage imageNamed:@"UserUpdationImageCell.png"]];
                    }



    return cell;
}
通过添加,您需要执行如下代码:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";

    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    PFObject *imageObject = [imgArry objectAtIndex:indexPath.row];
    PFFile *imageFile = [imageObject objectForKey:@"image4s"];
    NSString *UserProfile = [file url];

                    if([[SDWebImageManager sharedManager] diskImageExistsForURL:[NSURL URLWithString:UserProfile]])
                    {
                        NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:[NSURL URLWithString:UserProfile]];
                        UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key];
                        cell.imgView.image=image;
                    }
                    else
                    {
                        cell.imgView.image=[UIImage imageNamed:@"UserUpdationImageCell.png"];
                        [cell.imgView  sd_setImageWithURL:[NSURL URLWithString:UserProfile] placeholderImage:[UIImage imageNamed:@"UserUpdationImageCell.png"]];
                    }



    return cell;
}

当我滚动单元格中被另一个图像替换的集合视图图像时,出现了一个问题当我滚动单元格中被另一个图像替换的集合视图图像时,出现了一个问题