Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-09-2011, 03:36 PM   #1 (permalink)
Registered Member
 
aceiswild's Avatar
 
Join Date: Oct 2011
Location: Canada
Posts: 62
aceiswild is on a distinguished road
Default Accessing Documents Directory

Hey guys i am trying to retrieve an image that is stored in my documents directory but keep receiving an unused variable error.

here is the line of code i am using:
Code:
 NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *applicationDocumentsDirectory = [documentsPaths objectAtIndex:0];
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
__________________
Don't be a quitter! Never give up and keep pushing to succeed at the things you love!

Last edited by aceiswild; 12-09-2011 at 03:37 PM. Reason: typo
aceiswild is offline   Reply With Quote
Old 12-09-2011, 03:43 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Uh, so where is the code dealing with the image?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 12-09-2011, 03:49 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,005
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by aceiswild View Post
Hey guys i am trying to retrieve an image that is stored in my documents directory but keep receiving an unused variable error.

here is the line of code i am using:
Code:
 NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *applicationDocumentsDirectory = [documentsPaths objectAtIndex:0];
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?
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 12-09-2011, 03:49 PM   #4 (permalink)
Registered Member
 
aceiswild's Avatar
 
Join Date: Oct 2011
Location: Canada
Posts: 62
aceiswild is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Uh, so where is the code dealing with the image?


Sorry, in my firstViewController the image saves fine to documents.

Here is the code in my secondViewController to load the image:

Code:
- (void)viewDidLoad {
    // self.view.backgroundColor = [UIColor redColor];

    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:@"Documents/Test.png"]];

    

    NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *applicationDocumentsDirectory = [documentsPaths objectAtIndex:0];

    
    [super viewDidLoad];
}
Im trying to load the image as the secondViewControllers background. I can early set it as a colour but not an image.
__________________
Don't be a quitter! Never give up and keep pushing to succeed at the things you love!
aceiswild is offline   Reply With Quote
Old 12-09-2011, 03:51 PM   #5 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

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?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 12-09-2011, 04:12 PM   #6 (permalink)
Registered Member
 
aceiswild's Avatar
 
Join Date: Oct 2011
Location: Canada
Posts: 62
aceiswild is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
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!
aceiswild is offline   Reply With Quote
Old 12-09-2011, 04:17 PM   #7 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

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?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 12-09-2011, 04:41 PM   #8 (permalink)
Registered Member
 
aceiswild's Avatar
 
Join Date: Oct 2011
Location: Canada
Posts: 62
aceiswild is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
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.

secondViewController.M

Code:
- (void)viewDidLoad {

    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:@"Documents/Test.png"]];

   
    NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *applicationDocumentsDirectory = [documentsPaths objectAtIndex:0];
    

    [super viewDidLoad];
}
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!
aceiswild is offline   Reply With Quote
Old 12-09-2011, 04:45 PM   #9 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Why do you think that applicationDocumentsDirectory relates to the image in any way?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 12-09-2011, 04:49 PM   #10 (permalink)
Registered Member
 
aceiswild's Avatar
 
Join Date: Oct 2011
Location: Canada
Posts: 62
aceiswild is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Why do you think that applicationDocumentsDirectory relates to the image in any way?
Because i was thinking that is where the location of the images was saved.
So your saying, "applicationDocumentsDirectory" should be my image name?
__________________
Don't be a quitter! Never give up and keep pushing to succeed at the things you love!
aceiswild is offline   Reply With Quote
Old 12-09-2011, 04:57 PM   #11 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

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.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 12-09-2011, 05:53 PM   #12 (permalink)
Registered Member
 
aceiswild's Avatar
 
Join Date: Oct 2011
Location: Canada
Posts: 62
aceiswild is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
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!
aceiswild is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 412
13 members and 399 guests
7twenty7, AppsBlogger, David-T, Duncan C, EvilElf, HemiMG, heshiming, iekei, LunarMoon, Murphy, sacha1996, Sami Gh, teebee74
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,127
Posts: 402,915
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:45 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0