views:

508

answers:

2

I have an application, in which the user will select an image from a UIImagePickerView.

After selecting an image from it, I want to save it in my application.

How can this be achieved?

Thanks in advance for helping me.

+2  A: 

I would say something like this:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
self.resumePreviousSettingAfterEditing = true;
[self.topImageView setImage:image];
[cubeModel setImage:image forFace:[cubeModel.validFaces objectAtIndex:selectedRowInFacePicker]];

[self dismissImagePickerAnimated:true];

}

You register an event in your controller to handle the image selection. In that event handler, call a method somewhere, say in your model to set the new image. That function would look something like this:

 (void)saveImage:(UIImage *)image withName:(NSString *)imageName {
    // get the image path
    NSString *filename = [self determineImagePath:imageName];

    // make sure the file is removed if it exists
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:filename]) {
     if(NO == [fileManager removeItemAtPath:filename error:NULL]) {
     }
    }

    // Now, save the image to a file.
    if(NO == [UIImagePNGRepresentation(image) writeToFile:filename atomically:YES]) {
     [NSException raise:@"Image Save Failed" format:@"Unable to store image %s", filename]; 
    }
}

When you want to load the image again, you would so something like:

- (UIImage *)loadImage:(NSString *)imageName {
    NSString *filename = [self determineImagePath:imageName];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    self.currentImage = nil;
    if([fileManager fileExistsAtPath:filename]) {

     NSData *imageData = [[NSData alloc] initWithContentsOfFile:filename];
     UIImage *image = [UIImage imageWithData:imageData];

     self.currentImage = image;
    }
    return self.currentImage;
}

And don't get me started on transforming which is way harder than it should be.

Enjoy, Jacob

TheJacobTaylor
+2  A: 

Assuming you're using SDK 3.0, here is some code to save the image into your application's documents folder:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{ 
 // Dismiss the picker
 [[picker parentViewController] dismissModalViewControllerAnimated:YES];

 // Get the image from the result
 UIImage* image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];

 // Get the data for the image as a PNG
 NSData* imageData = UIImagePNGRepresentation(image);

 // Give a name to the file
 NSString* imageName = "MyImage.png";

 // Now, we have to find the documents directory so we can save it
 // Note that you might want to save it elsewhere, like the cache directory,
 // or something similar.
 NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString* documentsDirectory = [paths objectAtIndex:0];

 // Now we get the full path to the file
 NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];

 // and then we write it out
 [imageData writeToFile:fullPathToFile atomically:NO];

 return;
}
Itay