iphone - JSON Image Parsing/Prefixed URL -
i have established successful parsing of text , images in table view using json/php/mysql. storing location of images in database , actual images stored in directory on server. thing stored related image in database name. example car.jpg. want prefix url of image location on server can parsed without me having go db , manually entering url. here of code...
-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{ static nsstring *identifier = @"studentscell"; studentscell *cell = (studentscell *)[tableview dequeuereusablecellwithidentifier:identifier]; if (cell == nil) { nsarray *nib = [[nsbundle mainbundle] loadnibnamed:@"studentscell" owner:self options:nil]; cell = [nib objectatindex:0]; } nsdictionary *studentsdict = [students objectatindex:indexpath.row]; //i want prefix url key imagepath dont know , how it. nsurl *imageurl = [nsurl urlwithstring:[studentsdict objectforkey:@"imagepath"]]; nsdata *imagedata = [nsdata datawithcontentsofurl:imageurl]; uiimage *imageload = [[uiimage alloc] initwithdata:imagedata]; cell.imageview.image = imageload; nsstring *name = [nsstring stringwithformat:@"%@ %@", [studentsdict valueforkey:@"first"], [studentsdict valueforkey:@"last"]]; cell.title.text = name; nsstring *subtitle = [nsstring stringwithformat:@"%@", [studentsdict objectforkey:@"email"]]; cell.subtitle.text = subtitle; uibutton *button = [uibutton buttonwithtype:uibuttontypecustom]; button.frame = cgrectmake(265, 6, 44, 44); [button setimage:[uiimage imagenamed:@"email.png"] forstate:uicontrolstatenormal]; [button addtarget:self action:@selector(email:) forcontrolevents:uicontroleventtouchupinside]; [cell.contentview addsubview:button]; // cell.backgroundview = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@"cellbackground.png"]]; return cell; }
let's assume had like:
nsurl *baseurl = [nsurl urlwithstring:@"http://www.your.site.here/images"]; // whatever folder images
then do:
nsurl *imageurl = [baseurl urlbyappendingpathcomponent:[studentsdict objectforkey:@"imagepath"]];
by way, should consider using uiimageview
category, such sdwebimage. then, instead of loading nsdata
image data synchronously, can asynchronous image load:
[cell.imageview setimagewithurl:imageurl placeholderimage:[uiimage imagenamed:@"placeholder.png"]];
the placeholder should shown while image being loaded (perhaps blank image), , sdwebimage
asynchronously retrieve image , update cell when it's retrieved. yield far more responsive user interface. avail of image caching (so if scroll down , up, image won't retrieved again).
afnetworking has similar uiimageview
category, not quite robust of implementation. if you're using afnetworking, it's option.
Comments
Post a Comment