Tuesday 15 September 2015

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



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

i want parse image web services , show onto collection view custom cell write code as

in .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 .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, ^{ info = [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 { homecoming 1; } -(nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section { homecoming 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]; homecoming cell; }

then images web services parsed not shown collection view please give me solution.

try replace

-(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 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 utilize sdwebimagedownloader , within cellforrowatindexpath method, replace method cellforrowatindexpath

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"); }); }]; homecoming cell;

also import #import "uiimageview+webcache.h" in file

may help you.

ios json uicollectionview

No comments:

Post a Comment