The image was saved from my firstViewController and I'm trying to receive it from documents in my secondViewController.
I have been playing around with the following line of code but with no luck: imageWithContentsOfFile
Any suggestions?
Thanks,
SM
My suggestion is to learn how to write a complete description of your problem so people can help you. What is the whole method that is giving you a warning? What line is giving a warning, and what is the exact text of the warning?
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Ok, so you load the image using a fake path, and then create a path to a directory. Which part are you not understanding would lead to failure?
Basically i am not understanding how to load an image from the documents directory.
This is how i am getting my image and saving it in my firstViewController.m:
Code:
- (void)buttonPressed:(UIButton *)button
{
// Create image picker controller
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// Set source to the camera
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;;
// Delegate is self
imagePicker.delegate = self;
// Allow editing of image ?
imagePicker.allowsImageEditing = YES;
// Show image picker
[self presentModalViewController:imagePicker animated:YES];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSError *error;
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
// Create paths to output images
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"];
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];
// Write a UIImage to JPEG with minimum compression (best quality)
// The value 'image' must be a UIImage object
// The value '1.0' represents image compression quality as value from 0.0 to 1.0
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];
// Write image to PNG
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
// Let's check to see if files were successfully written...
// You can try this when debugging on-device
// Create file manager
NSFileManager *fileMgr = [NSFileManager defaultManager];
// Point to Document directory
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
// Write out the contents of home directory to console
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
// Dismiss the camera
[self dismissModalViewControllerAnimated:YES];
}
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
Steve
__________________
Don't be a quitter! Never give up and keep pushing to succeed at the things you love!
Ok, you're not getting my point. You are currently:
1. Loading an image
2. Creating a directory path
Isn't that backwards? And shouldn't the path be to a file?
Well what i was trying to do is open the photo album in my firstViewController, select an image, when the image is selected it saves it to the documentsDirectory for later use like in my secondViewController.
Okay i will restart fresh with my problem:
I have a button that opens the photo gallery, This is the code that i am using to open the photo gallery, select an image and save it to documentsDirectory:
firstViewController.M
Code:
- (void)buttonPressed:(UIButton *)button
{
// Create image picker controller
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// Set source to the camera
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;;
// Delegate is self
imagePicker.delegate = self;
// Allow editing of image ?
imagePicker.allowsImageEditing = YES;
// Show image picker
[self presentModalViewController:imagePicker animated:YES];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSError *error;
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
// Create paths to output images
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"];
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];
// Write a UIImage to JPEG with minimum compression (best quality)
// The value 'image' must be a UIImage object
// The value '1.0' represents image compression quality as value from 0.0 to 1.0
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];
// Write image to PNG
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
// Let's check to see if files were successfully written...
// You can try this when debugging on-device
// Create file manager
NSFileManager *fileMgr = [NSFileManager defaultManager];
// Point to Document directory
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
// Write out the contents of home directory to console
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
// Dismiss the camera
[self dismissModalViewControllerAnimated:YES];
}
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
Now in my secondViewController (view programatically created) i am trying to get that image that was saved to my documents directory and set it as the background in my secondViewController.
I am getting an error stating: "Unused variable 'applicationDocumentsDirectory' "
I am not quite sure how to get the image out of my documents directory to display in my secondViewController as the background. I know i need to declare something in my secondViewController.H which i am still working on.
Hope this makes a little more sense all put together,
Thanks,
Steve
__________________
Don't be a quitter! Never give up and keep pushing to succeed at the things you love!
I'm saying compare what you did to save it with what you are doing here. And I can't keep stressing enough that you don't create the path until AFTER you load the image. That makes no sense.
I'm saying compare what you did to save it with what you are doing here. And I can't keep stressing enough that you don't create the path until AFTER you load the image. That makes no sense.
Ok thanks I will change some stuff around and give it another go!
__________________
Don't be a quitter! Never give up and keep pushing to succeed at the things you love!