Ashish Gabani
Ashish Gabani

Reputation: 443

Parse an image in to the Collection-view Custom cell in ios

i want to parse an image from web services and show onto the collection view custom cell for this i write a code as

in my .h file

@property(strong,nonatomic)IBOutlet UICollectionView *imagecollection;
@property(strong,nonatomic)NSArray *imagesa;
@property(strong,nonatomic)NSDictionary *json;
@property(strong,nonatomic)NSArray *aimages;

and in my .m file

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)


#define imageURL [NSURL URLWithString:@"http://www.truemanindiamagazine.com/webservice/gallery_image.php"]

- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
    data = [NSData dataWithContentsOfURL: imageURL];
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});

[self.imagecollection registerNib:[UINib nibWithNibName:@"Custumcell" bundle:nil] forCellWithReuseIdentifier:@"CellIdentifier"];
}
-(void)fetchedData:(NSData *)responsedata
{
NSError* error;
self.json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
self.imagesa=[json objectForKey:@"data"];
NSLog(@"images,%@",self.imagesa);
}

 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.imagesa.count;
}
-(Custumcell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
 {
Custumcell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CellIdentifier" forIndexPath:indexPath];
UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,100,100)];
NSString *img2=[self.imagesa objectAtIndex:indexPath.row];
img.image=[UIImage imageNamed:img2];
cell.imageview.image=[UIImage imageNamed:img2];
return cell;
}

then images from web services is parsed but not shown into the collection view please give me any solution.

Upvotes: 2

Views: 849

Answers (2)

iHulk
iHulk

Reputation: 4909

try to replace your

-(void)fetchedData:(NSData *)responsedata
{
NSError* error;
self.json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
self.imagesa=[json objectForKey:@"data"];
NSLog(@"images,%@",self.imagesa);
}

with the code

-(void)fetchedData:(NSData *)responsedata
{
NSError* error;
self.json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
self.imagesa=[json objectForKey:@"data"];
if (self.imagesa.count) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [imagecollection reloadData];
            });
      }
NSLog(@"images,%@",self.imagesa);
}

now use SDWebImageDownloader and inside your cellForRowAtIndexpath method, replace your method cellForRowAtIndexPath with

Custumcell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CellIdentifier" forIndexPath:indexPath];
    NSDictionary *dict = [self.imagesa objectAtIndex:indexPath.item];
    NSString *img2=[dict valueForKey:@"link"];
    [cell.imageview sd_setImageWithURL:[NSURL URLWithString:[img2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[UIImage imageNamed:@"temp.png"] options:SDWebImageProgressiveDownload completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"downloaded");
        });
    }];
    return cell;

also import #import "UIImageView+WebCache.h" in your file

May be this will help you.

Upvotes: 1

Avi
Avi

Reputation: 11

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
-(Custumcell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Custumcell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CellIdentifier" forIndexPath:indexPath];
cell.imageview.image= nil; // or cell.poster.image = [UIImage imageNamed:@"placeholder.png"];

dispatch_async(kBgQueue, ^{
    NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[self.imagesa objectAtIndex:indexPath.row] objectForKey:@"link"]]];
    if (imgData) {
        UIImage *image = [UIImage imageWithData:imgData];
        if (image) {
            dispatch_async(dispatch_get_main_queue(), ^{
                Custumcell *updateCell = (id)[collectionView cellForItemAtIndexPath:indexPath];
                if (updateCell)
                    updateCell.imageview.image = image;
            });
        }
    }
});
return cell;
}

Upvotes: 0

Related Questions