Vaibhav
Vaibhav

Reputation: 35

How to fetch multiple images from server and save and show them in iPhone app?

I am using following code for getting single image. But, how to do it for multiple images?

This is my code :

-(void)viewDidLoad 
{

 [super viewDidLoad];

[self performSelectorInBackground:@selector(loadImageIntoMemory) withObject:nil];

}

-(void)loadImageIntoMemory {

    NSLog(@"Beginning download");
    NSString *temp_Image_String = [[NSString alloc] initWithFormat:@"http://www.thewavestore.com/appproductimage/"];
    NSURL *url_For_Ad_Image = [[NSURL alloc] initWithString:temp_Image_String];
    NSData *data_For_Ad_Image = [[NSData alloc] initWithContentsOfURL:url_For_Ad_Image];
    UIImage *temp_Ad_Image = [[UIImage alloc] initWithData:data_For_Ad_Image];
    [self saveImage:temp_Ad_Image];
    UIImageView *imageViewForAdImages = [[UIImageView alloc] init];
    imageViewForAdImages.frame = CGRectMake(0, 0, 320, 480);
    imageView.image = [self loadImage];
    [self.view addSubview:imageView];
}

-(void)saveImage:(UIImage *)image {

    NSLog(@"Saving");
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent: @"DSC02077129HT41.5W20L24WT18.555RS28775.jpg" ];
    NSData* data = UIImagePNGRepresentation(image);
    [data writeToFile:path atomically:YES];
}

-(UIImage *)loadImage {

    NSLog(@"Got the data!");
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:@"DSC02077129HT41.5W20L24WT18.555RS28775.jpg" ];
    UIImage* image = [UIImage imageWithContentsOfFile:path];
    return image;
}

Please help to over come from this issues,if any good idea please suggest me.

Upvotes: 2

Views: 319

Answers (2)

Kishore Kumar
Kishore Kumar

Reputation: 4375

1. Store the server images in to array.

    NsMutableArray *arrImages=[[nsmutableArray alloc]init];

  2.Get the count of the array to run this in for loop.



    for(int i=0;i<[arrImages count];i++)
     {
            NSLog(@"Saving");
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent: arrImages[i]];
    NSData* data = UIImagePNGRepresentation(image);
    [data writeToFile:path atomically:YES];
     }

To convert it to JPEG

    NSData=UIImageJPEGRepresentation(UIImage,1.0);

Upvotes: 0

Anshad Rasheed
Anshad Rasheed

Reputation: 2566

First save your images with unique names (i am using current time) and save this names to an array

NSData *imageData = UIImagePNGRepresentation(chosenImage);
    NSArray *paths = NSSearchPathForDirectoriesInDomains
    (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    // save image with currentdate and time in the name
    NSString * imageName = [NSString stringWithFormat:@"name_%@.png",[dateFormatter stringFromDate:[NSDate date]]];
    NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
    //add all the images names to the imagepath array
    [imagePathArray addObject:imageName];
    //write image into file
    [imageData writeToFile:fullPathToFile atomically:YES];

then retrive images from image path array

 for (NSString*path in imagePathArray ) {

            NSArray *filepart = [path componentsSeparatedByString:@"."];
            NSString *filename = [filepart objectAtIndex:0];
            NSString *extension = [filepart objectAtIndex:1];

            NSArray *paths = NSSearchPathForDirectoriesInDomains
            (NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *fileName = [NSString stringWithFormat:@"%@/%@",
                                  documentsDirectory,path];

            //NSString *filePath = [[NSBundle mainBundle] pathForResource:filename ofType:extension];
            NSData *fileData = [NSData dataWithContentsOfFile:fileName];
}

Upvotes: 1

Related Questions