-4

I have an app that downloads many images from a server. A main feature of the system is that the images can be viewed offline. The app platform is targeted for iPads ( the images are actually music lead sheets).

Where can I save these images and have them persist? I saw various posts that if you use the app Documnets folder then Apple will reject your app from iTunes. Since the feature of the app is that it works offline, an iCloud location would not work.

Several other posts mention to use cache or tmp directory but then the images may be garbage collected.

I am new to ios and build the app with swift. Any pointers or suggestions would be appreciated. Thx.

brewsky
  • 607
  • 1
  • 9
  • 15
  • 1
    What??? I don't really know where it's written, but other application, actually, are using **1GB** of their Documents folder! You can do it, don't worry – Luca D'Alberti Apr 12 '16 at 12:55
  • 1
    Possible duplicate of [How to store user data iOS](http://stackoverflow.com/questions/23740392/how-to-store-user-data-ios) – Caleb Apr 12 '16 at 12:58
  • There's really nothing specific to Swift here. – Caleb Apr 12 '16 at 13:01
  • 1
    There are so many answers to this online. http://stackoverflow.com/questions/19351535/saving-text-file-to-documents-directory-in-ios-7 – Jason Apr 12 '16 at 13:02
  • the `Documents` folder is for storing data permanently for the application; don't read 3rd party crap from keen amateurs, read the official documentation – the Apple has a really nice one. – holex Apr 12 '16 at 13:10
  • 1
    Read [Apple Documentation - Where You Should Put Your App’s Files](https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW28): There's nothing wrong with storing downloaded files in NSDocumentDirectory as long as you **exclude them from iCloud backup**. If you don't your app is likely to get rejected. – Lasse Apr 12 '16 at 13:10
  • Hey @brewsky You can do it no problem, you have to do 2 step for this. 1- Download the images form server and convert into NSData and write it on document directory with unique url. 2- Save this url in Local data base. Apple will not reject this process. I have already done it. – Parvendra Singh Apr 12 '16 at 13:18
  • Why won't you just use famous libraries designed for such purposes? Eg https://github.com/rs/SDWebImage no need to reinvent the wheel ;). Such images should be kept as cache. iOS will purge the memory only if user is critically low on space - the user would want to be able to get rid of such re-downloadable images in case of low memory. Really, keeping it permanently on disk will be horrible user experience! – Nat Apr 12 '16 at 13:18

3 Answers3

1
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *path =  [[paths objectAtIndex:0] stringByAppendingPathComponent:@"url.png"];

UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];

try it.

Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
iOS_Samboo
  • 11
  • 3
  • It'd be nice to use the language specified, but the question really isn't about language in any respect. The main problem with this answer is that the code obscures the important part of the answer, which is `NSDocumentsDirectory`. – Caleb Apr 12 '16 at 13:04
0
1.imageNamed.

2.Use  NSData  to load.

NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:extension]; 
NSData *image = [NSData dataWithContentsOfFile:filePath]; 
[UIImage imageWithData:image];
3. Use [UIImage imageWithContentOfFile:]  or  [image initWithContentOfFile:]
NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"imageName"]; 
[UIImage imageWithContentsOfFile:aImagePath];
Parvendra Singh
  • 965
  • 7
  • 19
iOS_Samboo
  • 11
  • 3
0

You can Save or Load anything the only thing you have to know is the ‘path’ you are going to use.(PS: folder will keep changing when using the IOS Simulator)

Save:

func saveImage (image: UIImage, filename: String) -> Bool{
  let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
  let filePath = documentsURL.URLByAppendingPathComponent(filename).path!
  let pngImageData = UIImagePNGRepresentation(image)
  let result = pngImageData!.writeToFile(filePath, atomically: true)
  return result
}

Usage: saveImage(myImage, filename: "hola.png")

Load:

func loadImageFromPath(filename: String) -> UIImage? {
    let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
    let filePath = documentsURL.URLByAppendingPathComponent(filename).path!
    let image = UIImage(contentsOfFile: filePath)
    if image == nil {
        print("missing image at: \(filePath)")
    }
    return image
}

Usage: loadImageFromPath("hola.png")

Med Abida
  • 1,214
  • 11
  • 31