Fetching image form documents

Discussion in 'iOS Programming' started by aceiswild, Dec 4, 2011.

  1. aceiswild, Dec 4, 2011
    Last edited: Dec 4, 2011

    aceiswild macrumors newbie

    Joined:
    Nov 15, 2011
    Location:
    Canada
    #1
    Hey guys, little question here, Been working on a different app then my last one but it is similar.

    I have an app that takes an picture and stores it in "Documents".
    Then in the next view controller it should load the image that was taken as the background but I am having some troubles loading the image as background in my next view.

    Here is my code...

    MainController.m: (take picture in this view)
    Code:
    - (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];
    }
    

    SecondViewController.m (should load the image as background in this view)
    Code:
    - (void)viewDidLoad {
    	// self.view.backgroundColor = [UIColor redColor];
    
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Documents/Test.png"]];
    	
    	self.tiles = [[NSMutableArray alloc] init];
    	
    	[self initPuzzle:@"myImage.png"];
        
        
        
        [super viewDidLoad];
    }
    
    
    My second view is not loading the picture that was taken in my first view as the background.

    Any help appreciated

    Thanks,
    Steve!
     
  2. cbougher, Dec 5, 2011
    Last edited: Dec 5, 2011

    cbougher macrumors member

    Joined:
    Oct 19, 2006
    Location:
    Atlanta, GA
    #2
    imageNamed will only return an image from the mainbundle. You need this method.

    Code:
    imageWithContentsOfFile:
    Creates and returns an image object by loading the image data from the file at the specified path.
    
    + (UIImage *)imageWithContentsOfFile:(NSString *)path
    Parameters
    path
    The full or partial path to the file.
    And pass the same path that you used to write the image.

    The Docs
     
  3. aceiswild thread starter macrumors newbie

    Joined:
    Nov 15, 2011
    Location:
    Canada
    #3
    Thanks for the reply cbougher,
    I knew there was somthing missing. I have used that method before but has been a very long time to remember what I did in the past! I will give this a shot when I can get on my computer.

    Steve
     
  4. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #4
    Code:
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    How did you decide to use this particular line? Do you see any problems with this approach?
     
  5. aceiswild thread starter macrumors newbie

    Joined:
    Nov 15, 2011
    Location:
    Canada
    #5
    Hey North Bronson,
    I have been in Vancouver all day so I will have to try when I get home. play around with the code abit. I'll let you know how it goes. Ate you doing somthing similar?
     
  6. aceiswild thread starter macrumors newbie

    Joined:
    Nov 15, 2011
    Location:
    Canada
    #6

    Would i add this line:
    Code:
    + (UIImage *)imageWithContentsOfFile:(NSString *)path;
    
    In the viewController.m where i am trying to load the image to?

    Steve
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    No. cbougher was simply presenting the definition of a class method. If you're not sure how to use that information they provided in your own code, perhaps you need to step away from the real coding and go learn the fundamentals of Objective-C programming.
     
  8. aceiswild thread starter macrumors newbie

    Joined:
    Nov 15, 2011
    Location:
    Canada
    #8
    No I get it's a definition. I've just never used it before and still getting the hang of saving and fetching data for later use. I always have to push harder and never step down or quit. Every bit of information that sombody gives me is helpful. I was at a technology institute for objective c Programming but could not finish due to having to undergo surgery. Since then everything else is on my own. I will admit my life in web design is alot easier but are both fun.

    Steve
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Well, in that case, do you know how to call a class method? Do you know how it differs from an instance method? Knowing that should help you to code the proper usage of the method cbougher has provided.
     
  10. aceiswild thread starter macrumors newbie

    Joined:
    Nov 15, 2011
    Location:
    Canada
    #10
    Calling a class and an instance

    I've been trying to call them something like this:

    Code:
     [B]H.[/B]
    @interface MyClass : NSObject {
          // Stuff
    }
    
    - (void)myInstance;
    + (void)myClass;
    
    
    Code:
      [B]M.[/B]
    [MyClass myClass];
    
    MyClass *object = [[MyClass alloc] init];
    [object myInstance];
    

    Steve
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    Alright, so how would you call UIImage's imageWithContentsOfFile: class method?
     
  12. aceiswild thread starter macrumors newbie

    Joined:
    Nov 15, 2011
    Location:
    Canada
    #12

    Im still playing around with things.
    Something kind of like this i quickly wrote. I know its not complete:

    M.

    Code:
    + (UIImage *)imageWithContentsOfFile:(NSString *)Documents {
    	UIImage *Image = [UIImage imageNamed:Documents];
    	
    
    }
    
    
    - (void)viewDidLoad {
    
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage     imageNamed:@"Documents/Test.png"]];
    
        [self imageWithContentsOfFile:@"Documents"];
    
        
        [super viewDidLoad];
    }
    
    
     
  13. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    imageWithContentsOfFile: is UIImage's class method. You don't need to, or want to, reimplement it. You just need to call it. You see how you are calling UIColor's colorWithPatternImage: class method, in this line of code?:
    Code:
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage     imageNamed:@"Documents/Test.png"]];
    You are also using a class method when you alloc something, and then an instance method when you init it.
    Code:
    ClassName *instanceName = [[ClassName alloc] init];
    Does that make sense?
     
  14. aceiswild thread starter macrumors newbie

    Joined:
    Nov 15, 2011
    Location:
    Canada
    #14

    Yes i had that same line (similar) before i added
    Code:
    + (UIImage *)imageWithContentsOfFile:(NSString *)Documents 
     
  15. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #15
    So, how are you going to call imageWithContentsOfFile: now?

    P.S. You'll still have some work to do to get the filePath set up properly.
     
  16. aceiswild thread starter macrumors newbie

    Joined:
    Nov 15, 2011
    Location:
    Canada
    #16
    Not sure yet,
    I know thats the part I'm playing around with still.
    Every other part of my project works fine except for the retrieval of images from a file.
     

Share This Page