Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load UIImage from documents directory

Tags:

ios

I'm trying to load a UIImage from the documents directory and set it to a UIImageView as per below:

NSString  *pngfile = [[MyUtil getLocalDirectory] stringByAppendingPathComponent:@"school.png"];
NSLog(@"%@", pngfile);
if ([[NSFileManager defaultManager] fileExistsAtPath:pngfile]) {
    NSData *imageData = [NSData dataWithContentsOfFile:pngfile];            
    UIImage *img = [UIImage imageWithData:imageData];
    [schoolImage setImage:img];
}

However, whenever I try the above, the image never loads. The image is in Documents/MyAppCustomDirectory/school.png. Is the above correct to load from that directory?

I also tried a few others: UIImage imageWithContentsOfFile, among other ways based on SO responses.

like image 756
KVISH Avatar asked Oct 24 '12 20:10

KVISH


People also ask

What is document directory in Swift?

The Documents directory is a better place to store data. We could append Documents to the path returned by the NSHomeDirectory() function, but it's a better idea to ask the FileManager class for the location of the Documents directory. This is slightly more verbose.

How do I save an image in a directory in Swift?

To write the image data to the Documents directory, we invoke the write(to:) method on the Data object. Because the write(to:) method is throwing, we wrap the method call in a do-catch statement and prefix it with the try keyword.

What is UIImage?

An object that manages image data in your app.


2 Answers

To get the documents directory you should use:

NSArray  *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir  = [documentPaths objectAtIndex:0];
NSString  *pngfile = [documentsDir stringByAppendingPathComponent:@"school.png"];

I'm not quite sure if you also need to append the 'MyAppCustomDirectory', but I don't think so.

like image 129
Tobi Avatar answered Sep 28 '22 18:09

Tobi


Swift 4 Solution:

guard let documentsDirectory = try? FileManager.default.url(for: .documentDirectory,
                                                                in: .userDomainMask,
                                                                appropriateFor:nil,
                                                                create:false)
                else {
                    // May never happen
                    print ("No Document directory Error")
                    return nil
                }

// Construct your Path from device Documents Directory
var imagesDirectory = documentsDirectory

// Only if your images are un a subdirectory named 'images'
imagesDirectory.appendPathComponent("images", isDirectory: true)

// Add your file name to path
imagesDirectory.appendPathComponent("school.png")

// Create your UIImage?    
let result = UIImage(contentsOfFile: imagesDirectory.path)
like image 40
Sébastien REMY Avatar answered Sep 28 '22 17:09

Sébastien REMY