Muhammad Salman
Muhammad Salman

Reputation: 553

Download images from parse and show in UICollection View

I am downloading the images from parse and after that I am showing that images in uicollection view but when I scroll the collection view it hangs. Here is my code

-(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];

        }

    }];

}

and Here is the code populating the images in collection view

- (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;
}

Upvotes: 0

Views: 50

Answers (2)

Nitin Gohel
Nitin Gohel

Reputation: 49730

By adding SDWebimage you need to do code like following:

- (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;
}

Upvotes: 1

Kim Jin
Kim Jin

Reputation: 180

Your download task may block the main thread.You can try to use SDWebImage or asynchrous download task and display image

Upvotes: 1

Related Questions