Search code examples
iosnsurlconnectionnsurlrequestnsmutableurlrequest

how can i get multiple images from server using url in iOS


Hi I am getting single image from server using url in iOS.

my code is like this

- (IBAction)overlaysClicked:(id)sender
{
    NSLog(@"overlays Clicked");
    request = [NSURLRequest requestWithURL:[NSURL         URLWithString:@"http://sukhada.co.in/img/overlays/neon/ov1.png"]];
    [NSURLConnection connectionWithRequest:request delegate:self];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *localFilePath = [documentsDirectory    stringByAppendingPathComponent:@"image.jpg"];

    NSData *thedata = [NSData dataWithContentsOfURL:[NSURL    URLWithString:@"http://sukhada.co.in/img/overlays/neon/ov1.png"]];
    [thedata writeToFile:localFilePath atomically:YES];

    UIImage *img = [[UIImage alloc] initWithData:thedata];

    self.overlayImgView.image=img;
}

To get multiple images from server my code like this

NSURL *myUrl = [NSURL URLWithString:@"http://sukhada.co.in/img/overlays/neon.zip"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
myData = [[NSMutableData alloc] initWithLength:0];
NSURLConnection *myConnection = [[NSURLConnection alloc] initWithRequest:myRequest delegate:self startImmediately:YES];

//my Array is like this in viewDidload

self.overlaysImgsArray = [[NSMutableArray alloc]initWithContentsOfURL:[NSURL    URLWithString:@"http://sukhada.co.in/img/overlays/neon.zip"]];
NSLog(@"urls is %@",overlaysImgsArray);
for (int i=0; i<[overlaysImgsArray count]; i++)  
//download array have url links
{
    NSURL *URL = [NSURL URLWithString:[overlaysImgsArray objectAtIndex:i]];
    NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]initWithURL:URL];
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
    {
        if([data length] > 0 && [[NSString stringWithFormat:@"%@",error] isEqualToString:@"(null)"])
        {
            //make your image here from data.
            UIImage *imag = [[UIImage alloc] initWithData:[NSData dataWithData:data]];
            NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *docDir = [array objectAtIndex:0];
            NSString *imgstr=[NSString stringWithFormat:@"%d",i];
            NSString *pngfilepath = [NSString stringWithFormat:@"%@sample%@.png",docDir,imgstr];
            NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(imag)];
            [data1 writeToFile:pngfilepath atomically:YES];
        }
        else if ([data length] == 0 && [[NSString stringWithFormat:@"%@",error] isEqualToString:@"(null)"])
        {
            NSLog(@"No Data!");
        }
        else if (![[NSString stringWithFormat:@"%@",error] isEqualToString:@"(null)"]){
            NSLog(@"Error = %@", error);
        }
    }];
    }
}

But this is not working for me please anybody suggest me how to get multiple images from server using one url which contains all the images. Please anybody thank you in advance


Solution

  • In your case you can try like this first you need to save all the 10 image in directory and the fetch one by one as your requirement .this code save all image from your url

    try this

    - (IBAction)overlaysClicked:(id)sender {
    
    
    //Note all your image saved with ov1.png,ov2.png......& so . 
    
           for (int i=1; i<=10; i++) {
    
        NSString *st2=@"ov";
        NSString *st1=@"http://sukhada.co.in/img/overlays/neon/ov";
        NSString *imageN=[st2 stringByAppendingString:[NSString stringWithFormat:@"%d",i]];
        NSString *imgNameforkey=[imageN stringByAppendingString:@".png"];
        NSString *url=[st1 stringByAppendingString:[NSString stringWithFormat:@"%d",i]];
    
        NSString *imgname=[url stringByAppendingString:@".png"];
        NSLog(@" all=%@",imgname);
    
    
        NSLog(@"overlays Clicked");
        request = [NSURLRequest requestWithURL:[NSURL         URLWithString:imgname]];
        [NSURLConnection connectionWithRequest:request delegate:self];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   NSUserDomainMask, YES);
    
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *localFilePath = [documentsDirectory    stringByAppendingPathComponent:imgname];
    
        NSData *thedata = [NSData dataWithContentsOfURL:[NSURL    URLWithString:imgname]];
        [thedata writeToFile:localFilePath atomically:YES];
    
    
        UIImage *img = [[UIImage alloc] initWithData:thedata];
    
        NSLog(@"imgs %@",img);
       // self.overlayImgView.image=img;
           }
    }
    

    here you can see all image saved enter image description here

    fetch the image

    using loop or with name

    NSArray *directoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *imagePath =  [directoryPath objectAtIndex:0];
    imagePath= [imagePath stringByAppendingPathComponent:@"ov1.png"];
    
     NSData *data = [NSData dataWithContentsOfFile:imagePath];
        UIImage *img = = [UIImage imageWithData:data];
    

    download a zip file from url and save it .use this code inside the button action. no need for loop now. try this

    - (IBAction)overlaysClicked:(id)sender {
        dispatch_queue_t queue = dispatch_get_global_queue(0,0);
        dispatch_async(queue, ^{
    
            NSLog(@"Beginning download");
            NSString *stringURL = @"http://sukhada.co.in/img/overlays/neonra.zip";
            NSURL  *url = [NSURL URLWithString:stringURL];
            NSData *urlData = [NSData dataWithContentsOfURL:url];
    
            //Find a cache directory. You could consider using documenets dir instead (depends on the data you are fetching)
            NSLog(@"Got the data!");
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
            NSString *path = [paths  objectAtIndex:0];
    
            //Save the data
            NSLog(@"Saving");
            NSString *dataPath = [path stringByAppendingPathComponent:@"img.zip"];
            dataPath = [dataPath stringByStandardizingPath];
            [urlData writeToFile:dataPath atomically:YES];
    
        });
    }
    

    and check you will get img.zip file enter image description here