Saturday, 25 January 2014

Download Images Asynchronously



Hi this is a simple code that used to download images from url in background.

  
    
    dispatch_queue_t imageLoadQueue = dispatch_queue_create("com.gmm.test",NULL);
    
    dispatch_async(imageLoadQueue, ^{
//Wait for 5 seconds...
        usleep(1000000);
        docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        
        
        for(int k=0; k <[allImage count] ;k++){
            
           
            imgURL = [allImage objectAtIndex:k];
            [imagePreview addObject:imgURL];
            imgData=[NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]];
           
            
            [imgData writeToFile:[NSString stringWithFormat:@"%@/%@", docPath, [imgURL lastPathComponent]] atomically:YES];
            
            
        }
//reload ur viewcontroller or tableview or collectionview  if necessary           
        
     
    });

    

No comments:

Post a Comment