How to save images inside app

556 Views Asked by At

Hi I'm new to iOS objective c. I'm working on a app which fetches images and stores it in the app memory for reference.

  1. when u click on the favourite it should go the favourites tab
  2. when clicked on downloads.The image should go on the download tab.

as ALAasset is deprecated can some one explain what is the best way to store images.

is it using file or PHImagemanager.

and how?

1

There are 1 best solutions below

1
Ketan Parmar On

You should store images in documents directory something like,

 NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];

NSString *documentsDirectoryForSaveImages = [docsDir stringByAppendingPathComponent:@"/ProfileImages"];

[[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectoryForSaveImages withIntermediateDirectories:YES attributes:nil error:nil];  // This creates folder in document directory of name "ProfileImages"

Now you can store image something like,

 NSData *data = UIImageJPEGRepresentation(responseObject, 1.0); //Convert image to data

 [data writeToFile:[documentsDirectoryForSaveImages stringByAppendingPathComponent:formImage] atomically:NO]; //write that data to path. formImage is string value (name of image. Ideally should use timestamp to make unique naming)

You can retrieve image something like,

   UIImage *img = [UIImage imageWithContentsOfFile:[documentsDirectoryForSaveImages stringByAppendingPathComponent:formImage]];

Hope this will help :)