Sunday, 15 January 2012

ios - XML image Loading issue in Xcode? -



ios - XML image Loading issue in Xcode? -

in application news displaying through xml parser , problem in xml url few images named standard arabic language images loading , if named english language (ex.dsc_5804.jpg) displaying fine , if named standard arabic (ex.لجنة%20تطوير%20الإعلام%20في%20الشورى%20تبدأ%20ف.jpg) not displaying , not sure issue image format or code or else can tel me solution please ??

you can see screen shot click here

image url link here

here newsviewcontroller.m

#import "newsviewcontroller.h" #import "newstableviewcell.h" #import "newdetailsviewcontroller.h" #import <sdwebimage/uiimageview+webcache.h> #import "searchvc.h" @interface newsviewcontroller () { nsstring *temstring; nsmutablestring *strtemp; bool isdatesearch; } @end @implementation newsviewcontroller @synthesize arrimages; @synthesize tittleone; @synthesize tittletwo; @synthesize tittlethree; @synthesize datepicker; @synthesize pickercontainer; @synthesize datepickertittle; - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { // custom initialization } homecoming self; } #pragma mark - view life cycle - (void)viewdidload { uiactivityindicatorview *spinnerview = [[uiactivityindicatorview alloc] initwithactivityindicatorstyle:uiactivityindicatorviewstylegray]; [super viewdidload]; [tittleone setfont: [uifont fontwithname:@"geeast-extrabold" size:12]]; [tittletwo setfont: [uifont fontwithname:@"geeast-extrabold" size:10]]; [tittlethree setfont: [uifont fontwithname:@"geeast-extrabold" size:10]]; // [[uicolor redcolor] set]; [datepickertittle setfont:[uifont fontwithname:@"geeast-extrabold" size:12]]; // [datepickertittle.textcolor= [uicolor yellowcolor]]; isdatesearch=no; self.arrtitles =[[nsmutablearray alloc] init]; self.arrdescription=[[nsmutablearray alloc]init]; self.arrimages=[[nsmutablearray alloc]init]; self.arrdate=[[nsmutablearray alloc]init]; self.arrurls=[[nsmutablearray alloc]init]; self.arrdatesearch=[[nsmutablearray alloc]init]; //[self performselectorinbackground:@selector(requestingfornews:) withobject:nil]; // dispatch_async( dispatch_get_global_queue(dispatch_queue_priority_default, 0), s^{ self.spinnerview.hidden=yes; [self makerequestfornews]; // self.spinnerview.stopanimating; //}); // additional setup after loading view. [self imagedownloader:@"http://www.shura.bh/mediacenter/news/"]; } -(void)requestingfornews:(id)sender { [self makerequestfornews]; } -(void) imagedownloader : (nsstring *)urlstringofimage { dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_high, 0ul); dispatch_async(queue, ^{ //downlaod image nsurl *imageurl = [nsurl urlwithstring:urlstringofimage]; nsdata *imagedata = [nsdata datawithcontentsofurl:imageurl]; uiimage *image = [uiimage imagewithdata:imagedata]; dispatch_async(dispatch_get_main_queue(), ^{ uiimageview *imageview = [[uiimageview alloc] initwithframe:cgrectmake(0, 0, 50, 50 )]; imageview.image = image; [self.view addsubview:imageview]; }); }); } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } #pragma mark - create request news -(void)makerequestfornews { nsurl *url =[nsurl urlwithstring:self.strnewsapi]; nsurlrequest *request = [nsurlrequest requestwithurl:url]; //after making request apparent thing expecting response may expected response or error. create objects , intialize them null. nsurlresponse *response = null; nserror *requesterror =null; //once have response , capture responce info using nsdata. nsdata *responsedata = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&requesterror]; //convert respnse info response string. nsstring *responsestring = [[nsstring alloc] initwithdata:responsedata encoding:nsutf8stringencoding]; //now can start parsing info using xml parser . need xml parser in-order utilize below class method "dictionaryforxmlstring". nserror *parsererror = null; //xml parsing nsxmlparser *xmlparser = [[nsxmlparser alloc] initwithdata:responsedata]; [xmlparser setdelegate:self]; [xmlparser parse]; // nsurl *url = [nsurl urlwithstring:url]; // nsdata *data = [nsdata datawithcontentsofurl:url]; // uiimage *image = [uiimage imagewithdata:data]; //nsdictionary *xmldict = [xmlreader dictionaryforxmlstring:responsestring error:null]; //once have xmldict handy, can pass viewcontroller (like table view) populate data. } - (void)parserdidenddocument:(nsxmlparser *)parser { } -(void)parser:(nsxmlparser *)parser didstartelement:(nsstring *)elementname namespaceuri:(nsstring *)namespaceuri qualifiedname:(nsstring *)qname attributes:(nsdictionary *)attributedict { if ([elementname isequaltostring:@"shuranews"]) { } if ([elementname isequaltostring:@"publishingpageimage"]) { } strtemp=[nsmutablestring new]; } -(void)parser:(nsxmlparser *)parser foundcharacters:(nsstring *)string { //temstring =string; [strtemp appendstring:string]; } -(void)parser:(nsxmlparser *)parser didendelement:(nsstring *)elementname namespaceuri:(nsstring *)namespaceuri qualifiedname:(nsstring *)qname { if ([elementname isequaltostring:@"title"]) { nslog(@"temstring=== %@", strtemp); [self.arrtitles addobject:strtemp]; } if ([elementname isequaltostring:@"publishingpagecontent"]) { nslog(@"temstring=== %@", strtemp); [self.arrdescription addobject:strtemp]; } if ([elementname isequaltostring:@"newsarticledate"]) { nslog(@"temstring=== %@", strtemp); [self.arrdate addobject:strtemp]; } if ([elementname isequaltostring:@"publishingpageimage"]) { nslog(@"tempimage=== %@", strtemp); [self.arrimages addobject:strtemp]; } if ([elementname isequaltostring:@"shuranews"]) { [self.tblnews reloaddata]; // self.spinnerview.hidden=yes; } if ([elementname isequaltostring:@"url"]) { [self.arrurls addobject:strtemp]; } } #pragma mark - tabeview datasource//delegate method -(nsinteger)numberofsectionsintableview:(uitableview *)tableview { homecoming 1; } -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { if (isdatesearch) { homecoming [self.arrdatesearch count]; } else{ homecoming [self.arrtitles count]; } } -(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { [tableview setseparatorinset:uiedgeinsetszero]; static nsstring *cellidentifier=@"cellnews"; newstableviewcell *cell=(newstableviewcell *)[tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath]; if (cell == nil) { cell = [[newstableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; // cell.newstableviewcell.textcolor = uicolorfromrgb(0x000000); cell.backgroundcolor=[uicolor clearcolor]; } if( [indexpath row] % 2){ cell.contentview.backgroundcolor =uicolorfromrgb(0xffffff); } else{ cell.contentview.backgroundcolor =uicolorfromrgb (0xdcdcdc); } //selectbackground color start uiview *newstableviewcell = [[uiview alloc] initwithframe:cell.frame]; newstableviewcell.backgroundcolor = uicolorfromrgb(0xdcdcdc); cell.selectedbackgroundview = newstableviewcell; //select background colro end cell.lbltitles.font = [uifont fontwithname:@"geeast-extrabold" size:12]; if (isdatesearch) { cell.lbltitles.text=[[self.arrdatesearch objectatindex:indexpath.row]objectforkey:@"title"]; } else{ cell.lbltitles.text=[self.arrtitles objectatindex:indexpath.row]; } cell.lbldescription.font =[uifont fontwithname:@"ge ss unique" size:12]; cell.lbldate.font=[uifont fontwithname:@"ge ss unique" size:12]; if (isdatesearch) { cell.lbldescription.text=[[self.arrdatesearch objectatindex:indexpath.row]objectforkey:@"des"]; } else{ cell.lbldescription.text=[self.arrdescription objectatindex:indexpath.row]; } cell.lbldate.text=[self.arrdate objectatindex:indexpath.row]; cell.lbltitles.textalignment= nstextalignmentright; cell.lbldate.textalignment = nstextalignmentright; cell.lbldescription.textalignment = nstextalignmentright; //sdwebimage code lazy loader [cell.imgnews setimagewithurl:[nsurl urlwithstring:[self.arrimages objectatindex:indexpath.row]] completed:^(uiimage *image, nserror *error, sdimagecachetype cachetype) { // if (![self.arrimages objectatindex:indexpath.row]) if ((cell.imgnews.image = image)) { cell.imgnews.layer.bordercolor = [uicolor blackcolor].cgcolor; cell.imgnews.layer.borderwidth = 2.0; } else { // if (!cell.imgnews ==nil) if (!cell.imgnews.image) { cell.lbltitles.frame=cgrectmake(cell.lbltitles.frame.origin.x, cell.lbltitles.frame.origin.y, 283, cell.lbltitles.frame.size.height); cell.lbldate.frame=cgrectmake(cell.lbldate.frame.origin.x, cell.lbldate.frame.origin.y, 286, cell.lbldate.frame.size.height); cell.lbldescription.frame=cgrectmake(cell.lbldescription.frame.origin.x, cell.lbldescription.frame.origin.y, 281, cell.lbldescription.frame.size.height); cell.imgnews.layer.bordercolor = [uicolor blackcolor].cgcolor; cell.imgnews.layer.borderwidth = 0; } } }]; // cell.imgnews.image=[uiimage imagewithdata:data]; // cell.imgnews.layer.bordercolor = [uicolor blackcolor].cgcolor; // cell.imgnews.layer.borderwidth = 2.0; [pickercontainer sethidden:yes]; // if (!cell.imgnews.image) // { // cell.lbltitles.frame=cgrectmake(cell.lbltitles.frame.origin.x, cell.lbltitles.frame.origin.y, 283, cell.lbltitles.frame.size.height); // cell.lbldate.frame=cgrectmake(cell.lbldate.frame.origin.x, cell.lbldate.frame.origin.y, 286, cell.lbldate.frame.size.height); // cell.lbldescription.frame=cgrectmake(cell.lbldescription.frame.origin.x, cell.lbldescription.frame.origin.y, 281, cell.lbldescription.frame.size.height); // cell.imgnews.layer.bordercolor = [uicolor blackcolor].cgcolor; // cell.imgnews.layer.borderwidth = 0; // } homecoming cell; } -(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { nsdictionary *dict=nil; if (isdatesearch) { //dict=[[nsdictionary alloc]initwithobjectsandkeys:[nsstring stringwithformat:@"%@",[[self.arrdatesearch objectatindex:indexpath.row]objectforkey:@"title"]],@"title",[nsstring stringwithformat:@"%@",[[self.arrdatesearch objectatindex:indexpath.row]objectforkey:@"des"]],@"img",[nsstring stringwithformat:@"%@",[self.arrdescription objectatindex:indexpath.row]],@"des",[nsstring stringwithformat:@"%@",[self.arrurls objectatindex:indexpath.row]],@"url", nil]; } else{ dict=[[nsdictionary alloc]initwithobjectsandkeys:[nsstring stringwithformat:@"%@", [self.arrtitles objectatindex:indexpath.row]],@"title",[nsstring stringwithformat:@"%@", [self.arrimages objectatindex:indexpath.row]],@"img",[nsstring stringwithformat:@"%@", [self.arrdescription objectatindex:indexpath.row]],@"des",[nsstring stringwithformat:@"%@",[self.arrurls objectatindex:indexpath.row]],@"url", nil]; } [self performseguewithidentifier:@"newsdetailsid" sender:dict]; } -(void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if ([segue.identifier isequaltostring:@"newsdetailsid"]) { ((newdetailsviewcontroller *)segue.destinationviewcontroller).strtitle=[sender objectforkey:@"title"]; ((newdetailsviewcontroller *)segue.destinationviewcontroller).strdetailimage=[sender objectforkey:@"img"]; ((newdetailsviewcontroller *)segue.destinationviewcontroller).strdescription=[sender objectforkey:@"des"];//strurl ((newdetailsviewcontroller *)segue.destinationviewcontroller).strurl=[sender objectforkey:@"url"]; } } - (ibaction)backbtnclicked:(id)sender { [self.navigationcontroller popviewcontrolleranimated:yes]; } - (ibaction)datepickerbt:(id)sender { [uiview beginanimations:nil context:null]; [uiview setanimationduration:0.3]; pickercontainer.frame = cgrectmake(0, 150, 320, 261); [pickercontainer sethidden:no]; } - (ibaction)hidebutton:(id)sender { [uiview beginanimations:nil context:null]; [uiview setanimationduration:0.3]; pickercontainer.frame = cgrectmake(0,600, 320, 261); [uiview commitanimations]; if ([self.arrdatesearch count]) { [self.arrdatesearch removeallobjects]; } if ([self.arrdate count]) { (int i=0; i<[self.arrdate count]; i++) { nslog(@"arrdate === %@",self.arrdate); nsarray *arrdatestr=[[self.arrdate objectatindex:i] componentsseparatedbystring:@" "]; nsarray *arrdat=[[nsstring stringwithformat:@"%@",[arrdatestr objectatindex:0]] componentsseparatedbystring:@"/"]; nsstring *strdat=[nsstring stringwithformat:@"%@-%@-%@",[arrdat objectatindex:2],[arrdat objectatindex:1],[arrdat objectatindex:0]]; nsstring *strpicdat=[[[nsstring stringwithformat:@"%@",self.datepicker.date]componentsseparatedbystring:@" "]objectatindex:0]; nslog(@" strpic date === %@",strpicdat); if ([strdat isequaltostring:strpicdat]) { isdatesearch=yes; nsdictionary *dicttemp=[nsdictionary dictionarywithobjectsandkeys:[self.arrtitles objectatindex:i],@"title",[self.arrdescription objectatindex:i],@"des",[self.arrimages objectatindex:i],@"img", nil]; [self.arrdatesearch addobject:dicttemp]; nslog(@"dates equal"); } } [self.tblnews reloaddata]; } } - (ibaction)reloadbutton:(id)sender { self.spinnerview.hidden=no; isdatesearch=no; [self makerequestfornews]; nslog(@"reloading!!!!!!!!!!!!!!!"); } @end

can seek next code

-(void) imagedownloader : (nsstring *)urlstringofimage { urlstringofimage = [urlstringofimage stringbyaddingpercentescapesusingencoding:nsasciistringencoding]; //or urlstringofimage = [urlstringofimage stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; // normal stuff doing. }

update

i think in case other method stringbyreplacingpercent rather stringbyaddingpercent should work, seek following

urlstringofimage = [urlstringofimage stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];

update2

the url gave not valid url see image, think should study webservice developer set valid url encoding, or improve alter webservice image urls, utilize of uniqueid or, something.

ios xcode uiimage

No comments:

Post a Comment