Amazon.com Widgets Retrieving an image stored in documents
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 20, 2013, 09:55:49 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  iOS Programming
| |-+  General Questions
| | |-+  Retrieving an image stored in documents
Pages: [1]   Go Down
Print
Author Topic: Retrieving an image stored in documents  (Read 848 times)
aceiswild
Newbie
*
Posts: 9






« on: December 06, 2011, 06:08:45 PM »

Hey guys,
I finished reading the parts on saving data. Currently i am working on an app that takes a picture and saves that picture to Documents as a png. All the saving stuff works perfect. Now in my secondViewController i am trying to retrieve this image from Documents when the view loads. So far i am not having any success with retrieving the saved image.
My XIB is created pragmatically.

in my secondViewController i am using this definition:
Code: (Objective-C)
+ (UIImage *)imageWithContentsOfFile:(NSString *)Documents;


I am not exactly sure what i need to write in my .H and .M files to use this definition.

Here is what i have so far:

secondViewController.H
Code: (Objective-C)
@interface SliderController : UIViewController {
   
     UIImage *viewTwoImage;

}

@property (nonatomic, retain) UIImage *viewTwo;

secondViewController.M
Code: (Objective-C)
#import "secondViewController.h"
#import "test_appViewController.h" // Imported my firstViewController where the image was taken from

@synthesize viewTwo;

@interface SliderController()

 + (UIImage *)imageWithContentsOfFile:(NSString *)Documents;

- (void)viewDidLoad {

 test_appViewController *viewTwo = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"myimagefile" ofType:@"png"]];
[self.initPuzzle.image setImage:viewTwo];

}

[super viewDidLoad];


Any other help on how to retrieve an image from Documents is very much appreciated!!

Thanks
SM
Logged
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #1 on: December 06, 2011, 10:11:03 PM »

How about:

Code: (Objective-C)
viewTwoImage = [UIImage imageNamed: @"myImagefile.png"];

Cheers,

Steve
Logged
aceiswild
Newbie
*
Posts: 9






« Reply #2 on: December 06, 2011, 10:36:47 PM »

How about:

Code: (Objective-C)
viewTwoImage = [UIImage imageNamed: @"myImagefile.png"];

Cheers,

Steve

Thanks,
So i should replace this line:
Code: (Objective-C)
test_appViewController *viewTwo = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"myimagefile" ofType:@"png"]];  
    [self.initPuzzle.image setImage:viewTwo]; 

With this line:
Code: (Objective-C)
viewTwoImage = [UIImage imageNamed: @"myImagefile.png"];

I will try it out. My problem is it is not retrieving the saved file from documents. But think I'm getting close to solving it.

Steven
Logged
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #3 on: December 07, 2011, 08:24:30 PM »

To be more precise,  you should replace that first line with this one:

Code: (Objective-C)
UIImage *viewTwo = [UIImage imageNamed: @"myImagefile.png"];

Cheers,

Steve
Logged
aceiswild
Newbie
*
Posts: 9






« Reply #4 on: December 07, 2011, 10:46:18 PM »

Darn thing still won't load the image

This is how i am getting a picture in my firstViewController and saving it. Does it seem correct?

firstViewController.M:

Code: (Objective-C)
- (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 = NO;
 
  // 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];
}







- (id)init
{
  if (self = [super init])
  {
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor grayColor];

    // Button to activate camera
    button = [[UIButton alloc] initWithFrame:CGRectMake(80, 55, 162, 53)];   
    [button setBackgroundImage:[UIImage imageNamed:@"Camera.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];     
    [self.view addSubview:button];
  }
 
  return self; 
}


@end


Im pretty sure my code above is correct for getting an image from the photo album and saving it to Documents. I thought this maybe could be a problem.

Thanks,
SM
Logged
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #5 on: December 07, 2011, 11:06:18 PM »

Okay, that makes a difference!  The imageNamed: method looks for the image in the application bundle, not  in directories like the Documents directory.  Use the imageWithContentsOfFile: method instead and give the same path you did to write the image out to the file.

Cheers,

Steve
Logged
aceiswild
Newbie
*
Posts: 9






« Reply #6 on: December 08, 2011, 12:01:18 AM »

Okay so i have been doing some changes and trying out the imageWithContentsOfFile method.

This is what i have come up with so far in my secondViewController.

secondViewController.M:

Code: (Objective-C)
- (void)viewDidLoad {
   
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:@"Documents/Test.png"]];

   
    [super viewDidLoad];
}


Do i need to import or define anything in my secondViewController.H, or can i just pull it straight out of the Documents directory? Dang i wish this was as simple as javascript and html!!!!

Thanks,
SM
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.