Please download the attached file for your reference.
In the view controller file.
there is two methods, one is for opening the iphone photo library
and the other one is didFinishPickingImage:editingInfo.
after choosing the image, it will the bring the user to the SomeEdit file with .xib file
in the someEdit file there is a UIImageView already.
but the question is I don't know how to link the two files together.
to show the image in the someEdit interface.
Please download the attached file for your reference.
In the view controller file.
there is two methods, one is for opening the iphone photo library
and the other one is didFinishPickingImage:editingInfo.
after choosing the image, it will the bring the user to the SomeEdit file with .xib file
in the someEdit file there is a UIImageView already.
but the question is I don't know how to link the two files together.
to show the image in the someEdit interface.
Make a UIImage a property of SomeEdit. Let's cal it imageToDisplay.
Then you can set the property from another view controller.
Code:
SomeEdit* theSomeEditViewController;
//Create a new view controller or point to an existing one
theSomeEditViewController.imageToDisplay = theImage;
[myNavigationController
pushViewController: theSomeEditViewController
animated: TRUE];
Then, in the SomeEdit view cotroller, put code in your viewWillAppear method that installs the image in the property into the image view.
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.
Make a UIImage a property of SomeEdit. Let's cal it imageToDisplay.
Then you can set the property from another view controller.
Code:
SomeEdit* theSomeEditViewController;
//Create a new view controller or point to an existing one
theSomeEditViewController.imageToDisplay = theImage;
[myNavigationController
pushViewController: theSomeEditViewController
animated: TRUE];
Then, in the SomeEdit view cotroller, put code in your viewWillAppear method that installs the image in the property into the image view.
Code:
//In the CameraChooseViewController.m
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo {
[picker dismissModalViewControllerAnimated:YES];
SomeEdit* theSomeEditViewController;
//Create a new view controller or point to an existing one
theSomeEditViewController.imageToShow = image;
[CameraChooseViewController pushViewController: theSomeEditViewController animated: TRUE];
}
// the SomeEdit.m file
- (void)viewDidLoad {
UIImage *img;
if (img != nil) { // Image was loaded successfully.
[img setImage:imageToShow];
[img release]; // Release the image now that we have a UIImageView that contains it.
}
[super viewDidLoad];
}
Here's the update version of it.
but still won't work
Can anyone point out where I go wrong?
Why are you releasing 'img'? UIImage's are not retained or released. Only the objects they are in and in certain situations. Try it without the [img release];
Also that code in the viewdidload should be after the super viewdidload line.
You shouldn't say they aren't retained or released. UIImages inherit from NSObject just like every class out there. It's just that in this instance, he hasn't retained it so he doesn't need to release it.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
//In the CameraChooseViewController.m
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo {
[picker dismissModalViewControllerAnimated:YES];
SomeEdit* theSomeEditViewController;
//Create a new view controller or point to an existing one
theSomeEditViewController.imageToShow = image;
[CameraChooseViewController pushViewController: theSomeEditViewController animated: TRUE];
}
// the SomeEdit.m file
- (void)viewDidLoad {
UIImage *img;
if (img != nil) { // Image was loaded successfully.
[img setImage:imageToShow];
[img release]; // Release the image now that we have a UIImageView that contains it.
}
[super viewDidLoad];
}
Here's the update version of it.
but still won't work
Can anyone point out where I go wrong?
This code doesn't make any sense:
Code:
// the SomeEdit.m file
- (void)viewDidLoad {
UIImage *img;
if (img != nil) { // Image was loaded successfully.
[img setImage:imageToShow];
[img release]; // Release the image now that we have a UIImageView that contains it.
}
[super viewDidLoad];
}
You create a local variable img. When you create a local variable in a method, the variable doesn't exist until the method is called, and it contains garbage once the method starts running.
Your img local variable will never contain a valid value when viewDidLoad is called. You want to look at the imageToShow property. So, viewDidLoad should look like this:
Code:
// the SomeEdit.m file
- (void)viewDidLoad
{
if (self.imageToShow != nil)
{
//Replace "myImageView" below with the name of your
//UIImageView IBOutlet that should display the image.
[myImageView setImage: self.imageToShow];
//[img release]; NO! DO NOT RELEASE anything here. No need.
}
[super viewDidLoad];
}
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.
but I notice there is one warning
it said that 'CameraChooseViewController' may not respond to '+pushViewController: animated:'
the whole source of the code is
Code:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo {
[picker dismissModalViewControllerAnimated:YES];
SomeEdit* theSomeEditViewController;
//Create a new view controller or point to an existing one
theSomeEditViewController.imageToShow = image;
[CameraChooseViewController pushViewController: theSomeEditViewController animated: TRUE];
}
but I notice there is one warning
it said that 'CameraChooseViewController' may not respond to '+pushViewController: animated:'
the whole source of the code is
Code:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo {
[picker dismissModalViewControllerAnimated:YES];
SomeEdit* theSomeEditViewController;
//Create a new view controller or point to an existing one
theSomeEditViewController.imageToShow = image;
[CameraChooseViewController pushViewController: theSomeEditViewController animated: TRUE];
}
"it still doesn't work" is not helpful. What happens? Have you stepped through your code and checked that the "import" variable is not nil? Have you checked that imageToShow is not nil? Where is the code that sets the imageToShow property and then displays the view controller?
As for the warning you're getting, it sounds like you're trying to send a pushViewController: animated: message to a view controller. You send that message to your navigation controller, not a view controller. That will crash your program at runtime.
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.
"it still doesn't work" is not helpful. What happens? Have you stepped through your code and checked that the "import" variable is not nil? Have you checked that imageToShow is not nil? Where is the code that sets the imageToShow property and then displays the view controller?
As for the warning you're getting, it sounds like you're trying to send a pushViewController: animated: message to a view controller. You send that message to your navigation controller, not a view controller. That will crash your program at runtime.
This is the code I set my UIImageView *import to UIImage *imageToShow
and it is how I check there is imageToShow is nil or not.
Please point out my mistake.
For the second question, I really don't know how to send the message to a view controller. As you can see, I'm really new to objective c and iphone apps programming.
Is it like this?
Code:
SomeEdit* theSomeEditViewController = [[SomeEdit alloc] initWithNibName:@"SomeEdit" bundle:[NSBundle mainBundle]];
//Create a new view controller or point to an existing one
theSomeEditViewController.imageToShow = image;
[self.navigationController pushViewController: theSomeEditViewController
animated: TRUE];
[theSomeEditViewController release];
}
But the imageView is still not the image I choose from the iPhone Library.
It go back to the home page, which is the CameraChoose xib