ios - Asynchronous loading of images? -
how can load images asynchronously? i'm doing (borrowed open source code loading image thumbnail):
- (uiimage*)thumbnaillistview:(thumbnaillistview*)thumbnaillistview imageatindex:(nsinteger)index { nsurl* url = [nsurl urlwithstring:[recommendedarray objectatindex:index]] ; // [[cell grid_image] setimage:[uiimage imagewithdata:[nsdata datawithcontentsofurl:url]]]; nsstring *docpath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; bool isimageloaded = yes; uiimage *bookimage = [uiimage imagewithcontentsoffile:[nsstring stringwithformat:@"%@/%@", docpath, [[recommendedarray objectatindex:index] lastpathcomponent]]]; if(bookimage == nil) isimageloaded = no; if(!isimageloaded){ uiimage *thumbnailimage =[uiimage imagenamed:@"app-icon-144x144.png"];} else{ uiimage *thumbnailimage = [uiimage imagewithdata:[nsdata datawithcontentsofurl:[nsurl urlwithstring:[nsstring stringwithformat:@"%@",[recommendedarray objectatindex:index]]]]]; return thumbnailimage; } }
i tried adding following method, it's producing error nsurl:
dispatch_queue_t imageloadqueue = dispatch_queue_create("com.g.load", null); dispatch_async(imageloadqueue, ^{ //wait 5 seconds... usleep(1000000); nsarray *docpath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; for(int k=0; k < [images count];k++){ nslog(@"loading %d image", k); nsstring *imgurl = [images objectatindex:k]; nsdata *imgdata = [[nsdata alloc] initwithcontentsofurl:[nsurl urlwithstring:imgurl]]; [imgdata writetofile:[nsstring stringwithformat:@"%@/%@", docpath, [imgurl lastpathcomponent]] atomically:yes]; } [[self collectionview] reloaddata]; nslog(@"done images"); });
error
[nsurl length]: unrecognized selector sent instance 0x12ea7240 2013-04-18 11:18:35.456 assamkart[3084:14d03] *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[nsurl length]: unrecognized selector sent instance 0x12ea7240'
inside method->
- (uiimage*)thumbnaillistview:(thumbnaillistview*)thumbnaillistview imageatindex:(nsinteger)index
use this->
nsurlconnection *ansurlconnection= [[nsurlconnection alloc]initwithrequest:<#(nsurlrequest *)#> delegate:<#(id)#> startimmediately:<#(bool)#>];
and inside delegate
- (void)connectiondidfinishloading:(nsurlconnection *)connection;
image data , implement logic , here u shall call again the
- (uiimage*)thumbnaillistview:(thumbnaillistview*)thumbnaillistview imageatindex:(nsinteger)index
method recursively till recommendedarray
exhausted.
Comments
Post a Comment