Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing images locally on an iOS device

Some iOS photo-related apps store images created with the app somewhere other than the photo library. For example Fat Booth shows a scrolling list of photos created with the app at app start-up. Note these photos are persisted w/o the user explicitly saving them to the photo library. What is the simplest way to save and persist images inside an iOS application?

The only persistent stores I'm familiar with are NSUserDefaults and the key chain. However I've never heard of these used to store larger pieces of data such as an image. Now I'm wondering if Core Data is the easiest way.

like image 446
SundayMonday Avatar asked Jan 25 '13 23:01

SundayMonday


People also ask

How do I save images on my iPhone?

On your iPhone, iPad, or iPod touchOpen the Photos app, then go to the Albums tab. Scroll down to Shared Albums and select an album. Tap the photo or video, then tap the share button . Choose Save Image or Save Video.

Where are photos physically stored on iPhone?

Answer: A: By default Photos saves its library in your Pictures folder. If you want to know the exact location you can open Photos and navigate to Preferences / click on General tab and it will show you the location.

Why can't I save a photo on my iPhone?

Method 1: Check your iPhone storage If there is not enough space for new files, iPhone not saving photos to camera roll will definitely appear. You can try to free up space on iPhone by deleting useless apps, delete photos, music, notes, videos, and messages. Go to "Settings" > "General" > "iPhone Storage".


2 Answers

The simplest way is to save it in the app's Documents directory and save the path with NSUserDefaults like so:

NSData *imageData = UIImagePNGRepresentation(newImage);  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0];  NSString *imagePath =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",@"cached"]];  NSLog(@"pre writing to file"); if (![imageData writeToFile:imagePath atomically:NO])  {     NSLog(@"Failed to cache image data to disk"); } else {     NSLog(@"the cachedImagedPath is %@",imagePath);  } 

Then save the imagePath in some dictionary in NSUserDefaults or however you'd like, and then to retrieve it just do:

 NSString *theImagePath = [yourDictionary objectForKey:@"cachedImagePath"];  UIImage *customImage = [UIImage imageWithContentsOfFile:theImagePath]; 
like image 117
Tommy Devoy Avatar answered Sep 23 '22 09:09

Tommy Devoy


For Swift:

let imageData = UIImagePNGRepresentation(selectedImage) let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String let imagePath = paths.stringByAppendingPathComponent("cached.png")  if !imageData.writeToFile(imagePath, atomically: false) {    println("not saved") } else {    println("saved")    NSUserDefaults.standardUserDefaults().setObject(imagePath, forKey: "imagePath") } 

For Swift 2.1:

let imageData = UIImagePNGRepresentation(selectedImage) let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] let imageURL = documentsURL.URLByAppendingPathComponent("cached.png")  if !imageData.writeToURL(imageURL, atomically: false) {     print("not saved") } else {     print("saved")     NSUserDefaults.standardUserDefaults().setObject(imageData, forKey: "imagePath") } 

stringByAppendingPathComponent is unavailable in Swift 2.1 so you can use URLByAppendingPathComponent. Get more info here.

like image 25
Segev Avatar answered Sep 23 '22 09:09

Segev