0

i wanna download an image in my document file and display it again using image path in the document file

how could i download an image into document file in iphone simulator and display it again by it's path in UIImageView?

  • 2
    possible duplicate of [iOS: Download image from url and save in device](http://stackoverflow.com/questions/2499176/ios-download-image-from-url-and-save-in-device) – Misha Sep 10 '15 at 10:27

2 Answers2

0

For downloading the image you can use this code

- (IBAction) downloadImg : (id) sender {
UIImageWriteToSavedPhotosAlbum(largeImg.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); 
}

- (void) image:(UIImage*)image didFinishSavingWithError:(NSError *)error contextInfo:(NSDictionary*)info 
{
  if (error != NULL)

{
    [[[UIAlertView alloc] initWithTitle:nil message:@"Some error has been occured.\n Please try again later" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];        
}

else
{
    [[[UIAlertView alloc] initWithTitle:nil message:@"Image saved successfully\n in your gallery." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
} 
}
}
Simran
  • 42
  • 7
0

Try this code may be it's help you.

NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.webdesignmash.com/trial/wp-content/uploads/2010/06/why-i-love-typography1.jpg"]];  // it's just example url no use to personally.
UIImage *imageFromURL = [UIImage imageWithData:data];
[UIImagePNGRepresentation(imageFromURL) writeToFile:[documentsDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", @"Test"]] options:NSAtomicWrite error:nil];
UIImage * imageFromWeb = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.png", documentsDirectoryPath, @"Test"]];
img.image=imageFromWeb; // img is your imageview

Some information to help you to understood this code like:

1) Test : You can change your image name insted of "Test".

2). Image URL :- You can add your image URL.

3). img : it's your UIImageView name Like this (IBOutlet UIImageView *img;)

Note:- Show image in your UIImageview may be take few times so wait for it. or integrate Loader.

Sandy Patel
  • 768
  • 7
  • 19