iphone - how to put the JSON data into UICollectionView in ios -
i try images json url , place on uicollectionview
but don't how images json
#import "viewcontroller.h" #import "customcell.h" @interface viewcontroller () { nsarray *arrayofimages; nsarray *arrayofdescriptions; nsmutablearray *json; nsstring *img; nsmutabledata *webdata; nsurlconnection *connection; } @end @implementation viewcontroller - (void)viewdidload { /**/ [[self mycollectionview]setdatasource:self]; [[self mycollectionview]setdelegate:self]; nsstring *urlstring=[nsstring stringwithformat:@"http://ielmo.xtreemhost.com/array.php"]; nsurl * url=[nsurl urlwithstring:urlstring]; nsurlrequest *req=[nsurlrequest requestwithurl:url]; connection=[nsurlconnection connectionwithrequest:req delegate:self]; if(connection) { nslog(@"connected"); webdata=[[nsmutabledata alloc]init]; } [super viewdidload]; // additional setup after loading view, typically nib. } -(void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response { [webdata setlength:0]; } -(void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data { [webdata appenddata:data]; } -(void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error { nslog(@"error is"); } -(void)connectiondidfinishloading:(nsurlconnection *)connection { nsmutablearray *al=[nsjsonserialization jsonobjectwithdata:webdata options:0 error:nil]; nslog(@"all array isn %@",al); nsdata* mydata = [nskeyedarchiver archiveddatawithrootobject:al]; nslog(@"data data%@",mydata); nserror *error; json=(nsmutablearray*)[nsjsonserialization jsonobjectwithdata:mydata options:kniloptions error:&error]; nslog(@"data arrayjson%@",json); for(int i=0;i<json.count;i++) { img=[nsstring stringwithformat:@"%@",[json objectatindex:i]]; nslog(@"data arrayimage%@",img); } nsurl *urlone=[nsurl urlwithstring:img]; nslog(@"data arrayurl%@",urlone); nsdata *newdata=[nsdata datawithcontentsofurl:urlone]; uiimageview *imaegview=[[uiimageview alloc]initwithframe:cgrectmake(38,0, 76, 96)]; [imaegview setimage:[uiimage imagewithdata:newdata]]; [self.mycollectionview addsubview:imaegview]; [[self mycollectionview]reloaddata]; } -(nsinteger)numberofsectionsincollectionview:(uicollectionview *)collectionview{ return 1; } -(nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section{ return [arrayofimages count]; } -(uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath{ static nsstring *cellidentifier=@"cell"; customcell *cell=[ collectionview dequeuereusablecellwithreuseidentifier:cellidentifier forindexpath:indexpath]; [[cell myimage]setimage:[uiimage imagenamed:[arrayofimages objectatindex:indexpath.item]]]; // [[cell mylabel]settext:[arrayofdescriptions objectatindex:indexpath.item]]; return cell; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } @end
i tried code in output image not display in collectionview
please tell me how retrieve images json my-collection view
as json
contains image.
it must encoded while transmitting in base64. need decode form nsdata
.
once have converted nsdata
can form uiimage
it, after can show image on collection view.
Comments
Post a Comment