iphone - Async image in button issue -
i have piece of code in function. , looping creating async image inside button. when run it, button not show async image.please tell me what's wrong code. when print nslog url, going , show of image url.
// add button uibutton *btn = [uibutton buttonwithtype:uibuttontypecustom]; [btn setframe:cgrectmake(topleft.x,topleft.y,width,height)]; //asyncronous image nsmutablestring *imageurl; if ([[[type objectatindex:btnnumber] objectforkey:@"last_content_image"] isequaltostring:@""] || [[type objectatindex:btnnumber] objectforkey:@"last_content_image"] == nil) { imageurl = [nsmutablestring stringwithformat:@"bank_images/actual/"]; }else{ imageurl = [[type objectatindex:btnnumber] objectforkey:@"last_content_image"]; } nsurl *url1 = [nsurl urlwithstring:imageurl]; nslog(@"url gbr : %@", url1); nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *stringsub = [imageurl substringwithrange:nsmakerange (imageurl.length-9, 9)]; nsstring *path = [nsstring stringwithformat:@"%@/%u_%@", documentsdirectory,[globalvariable sharedinstance].indexcategoryglobal,stringsub]; if ([[nsfilemanager defaultmanager] fileexistsatpath:path]) { nsdata *img = [nsdata datawithcontentsoffile:path]; uiimage *imageexist = [uiimage imagewithdata:img]; image = imageexist; [btn setimage:image forstate:uicontrolstatenormal]; }else{ dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_high, 0); dispatch_async(queue, ^{ //this load lazily nsdata *data1 = [nsdata datawithcontentsofurl:url1]; dispatch_sync(dispatch_get_main_queue(), ^{ image = [uiimage imagewithdata:data1]; [btn setimage:image forstate:uicontrolstatenormal]; if ([stringsub isequaltostring:@"s/actual/"]){ image = [uiimage imagenamed:@"default_thumb.png"]; [btn setimage:image forstate:uicontrolstatenormal]; } [[nsfilemanager defaultmanager] createfileatpath:path contents:data attributes:nil]; }); }); } [btn settag:btnnumber];
looks create nsurl object in wrong way apple documentation says that
urlwithstring: creates , returns nsurl object initialized provided string.
- (id)urlwithstring:(nsstring *)urlstring parameters urlstring string initialize nsurl object. must url conforms rfc 2396. method parses urlstring according rfcs 1738 , 1808. (to create nsurl objects file system paths, use fileurlwithpath:isdirectory: instead.)
so, looks should use fileurlwithpath:isdirectory:
Comments
Post a Comment