Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

aceiswild

macrumors newbie
Original poster
Nov 15, 2011
29
0
Canada
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!
 
Last edited:
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
 
Last edited:
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

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
 
Code:
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

How did you decide to use this particular line? Do you see any problems with this approach?
 
Code:
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

How did you decide to use this particular line? Do you see any problems with this approach?

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?
 
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


Would i add this line:
Code:
+ (UIImage *)imageWithContentsOfFile:(NSString *)path;

In the viewController.m where i am trying to load the image to?

Steve
 
Would i add this line:
Code:
+ (UIImage *)imageWithContentsOfFile:(NSString *)path;

In the viewController.m where i am trying to load the image to?
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.
 
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.

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
 
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.
 
Calling a class and an instance

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.

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
 
Alright, so how would you call UIImage's imageWithContentsOfFile: class method?


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];
}
 
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?
 
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?


Yes i had that same line (similar) before i added
Code:
+ (UIImage *)imageWithContentsOfFile:(NSString *)Documents
 
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.
 
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.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.