ios - Lazy loading in UITableView crashes app -
i using uitableview customcell. customcell contains uiimageview , 2 uilabel. in uiimageview want load image url , want save in document directory.
here code tried,
nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory,nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *savedimagepath = [documentsdirectory stringbyappendingpathcomponent:[urlarr objectatindex:indexpath.row]];//arr nsmutablearray of image url uiimage *img1 = [uiimage imagewithcontentsoffile:savedimagepath]; if (!img1 || [uiimagepngrepresentation(img1) length] <=0) { id path =[urlarr objectatindex:indexpath.row]; path = [path stringbyreplacingoccurrencesofstring:@" " withstring:@"%20"]; nsurl *url = [nsurl urlwithstring:path]; nsmutablearray *arr = [[nsmutablearray alloc] initwithobjects:url, [nsstring stringwithformat:@"%d", indexpath.row+1], nil ]; [self performselectorinbackground:@selector(loadimageinbackground:) withobject:arr]; } and 1 too.
//—————————————-lazy loading———————– - (void)loadimageinbackground:(nsarray *)urlandtagreference { nsdata *imgdata = [nsdata datawithcontentsofurl:[urlandtagreference objectatindex:0]]; uiimage *imgload = [[uiimage alloc] initwithdata:imgdata]; nsmutablearray *arr = [[nsmutablearray alloc] initwithobjects:imgload, [urlandtagreference objectatindex:1], nil]; [self performselectoronmainthread:@selector(assignimagetoimageview:) withobject:arr waituntildone:yes]; } - (void) assignimagetoimageview:(nsarray *)imgandtagreference { nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory,nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *savedimagepath = [documentsdirectory stringbyappendingpathcomponent:[urlarr objectatindex:[[imgandtagreference objectatindex:1] intvalue]-1]]; uiimage* imagetosave = savedimagepath; nsdata *imagedata = uiimagepngrepresentation(imagetosave); [imagedata writetofile:savedimagepath atomically:no]; } reason crashing shows nsinvalidargumentexception. image not showing in uitableviewcell. appreciated.
for lazy loading of table view images there few options available. can make use of them in design save time , avoid efforts reinvent wheel.
1. apple lazy loading code --link
2. sdwebimage --link
cheers! amar.
Comments
Post a Comment