Josh Kahane
Josh Kahane

Reputation: 17169

Saving and loading UIImage in an iPhone app

I am having a headache working out what's wrong with my app. I am trying to save and load UIImages, simple right? Seems not so.

I have a class with these methods to save, load and delete an image with a unique string. Just how I like it, nice and simple. Here is the class:

imageSavedLoad.h:

#import <Foundation/Foundation.h>

@interface imageSaveLoad : NSObject

- (void)saveImage:(UIImage*)image forKey:(NSString*)imageName;
- (void)removeImage:(NSString*)fileName;
- (UIImage*)loadImage:(NSString*)imageName;

@end

imageSaveLoad.m:

#import "imageSaveLoad.h"


@implementation imageSaveLoad

//saving an image
- (void)saveImage:(UIImage*)image forKey:(NSString*)imageName
{
    NSData *imageData = UIImagePNGRepresentation(image);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]];
    [fileManager createFileAtPath:fullPath contents:imageData attributes:nil];
    NSLog(@"image saved");
}

//removing an image
- (void)removeImage:(NSString*)fileName
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", fileName]];
    [fileManager removeItemAtPath: fullPath error:NULL];
    NSLog(@"image removed");
}

//loading an image
- (UIImage*)loadImage:(NSString*)imageName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]];
    return [UIImage imageWithContentsOfFile:fullPath];
}

@end

Where I want to save, load or delete an image I import the class: #import "imageSaveLoad.h"

Then I create an instance of it and call the required methods (here I save):

imageSaveLoad *imgSL = [[imageSaveLoad alloc] init];
[imgSL saveImage:photoImgView.image forKey:@"yourPhoto"];

Then in another view/class where I want to retrieve the image:

imageSaveLoad *imgSL = [[imageSaveLoad alloc] init];
yourImgView.image = [imgSL loadImage:@"yourPhoto"];

But yourImgView displays nothing, blank.

So where am I going wrong? I have checked all IBOutlet connections and that all the code is being called. Is there a problem with the methods I am using?

Upvotes: 1

Views: 1212

Answers (1)

8Ours
8Ours

Reputation: 132

When saving image, try by replacing this line :

 [fileManager createFileAtPath:fullPath contents:imageData attributes:nil];

With

 [imageData writeToFile:fullpath atomically:YES];

Upvotes: 1

Related Questions