Henry F
Henry F

Reputation: 4980

iOS: Converting An Image To PNG To Allow For Saving

The app that I'm working on allows the user to pick an image from their camera roll, and display it in a UIImageView. Now, I'm having some issues trying to save the data. I know that I must convert the image to PNG and save the data, but I'm having some issues doing that. I'm very new to file saving and Objective-C in general. Here is the code I'm using:

- (NSString *)dataFilePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(
                                                         NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:kFilename9];
}

- (void)applicationDidEnterBackground:(NSNotification *)notification {
    NSMutableArray *array = [[NSMutableArray alloc] init];

    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image.image)];
    [array addObject:imageData];

    [array writeToFile:[self dataFilePath] atomically:YES];
}
- (void)viewDidLoad
{
    self.imgPicker = [[UIImagePickerController alloc] init];
    self.imgPicker.allowsImageEditing = YES;
    self.imgPicker.delegate = self;
    self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;


    NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        image.image = [array objectAtIndex:0];
    }

    UIApplication *app = [UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationDidEnterBackground:)
                                                 name:UIApplicationDidEnterBackgroundNotification
                                               object:app];

    [super viewDidLoad];

}

The program crashed upon simply tapping the tab that this takes place in. The debugger states "'NSInvalidArgumentException', reason: '-[__NSCFData _isResizable]: unrecognized selector sent to instance 0x16cf50'". Any help is much appreciated, thank you!

Upvotes: 1

Views: 2521

Answers (2)

Liftoff
Liftoff

Reputation: 25372

Okay, first things first, you are getting this error the second you are entering the "tab" that does these operations, yes? So the error should be in your viewDidLoad method. Second, your error calls for a selector error, so find the place where you used @selector.

Here is your problem that is causing the error:

[[NSNotificationCenter defaultCenter] addObserver:self
                                            selector:@selector(applicationDidEnterBackground:)
                                             name:UIApplicationDidEnterBackgroundNotification
                                           object:app];

Notice that the method applicationDidEnterBackground has a parameter (NSNotification*):

- (void)applicationDidEnterBackground:(NSNotification *)notification {

and yet you are sending it app, which is a UIApplication:

UIApplication *app = [UIApplication sharedApplication]

This is the only thing that I can see would be giving you this error. Just change it to this and the error should go away:

- (void)applicationDidEnterBackground:(UIApplication*)application {

BEGIN EDIT:

As far as saving the image, I don't know exactly how you are storing the data, but here's how I would do it.

  1. Use a retained property to store the data of the image.

ViewController.h

@interface ViewController

//Add this line
@property(retain) NSData* imageData;

@end

ViewController.m

//Remember to include this line somewhere in this file:
@synthesize imageData;

When you are going to save the file:

- (void)applicationDidEnterBackground:(UIApplication*)application {
self.imageData = [NSData dataWithData:UIImagePNGRepresentation(image.image)];

[self.imageData writeToFile:[self dataFilePath] atomically:YES];

}

This way there is no way that you will lose the data, and you don't need to use an array. Just make sure to add this line as well in the viewDidUnload method:

self.imageData = nil;

END EDIT

Hope that helps you.

Upvotes: 1

Adam Shiemke
Adam Shiemke

Reputation: 3742

Provided yourUIImage is valid and contains data, you can just call

[imageData writeToFile:(NSString*) atomically(BOOL)]

No need to create an array and write to it.

Upvotes: 0

Related Questions