Reputation: 4980
I'm very new to Objective-C, and am having some beginner issues. I have an application that has an area that is supposed to behave somewhat like a photo gallery. You click the "Add Image" button, and the image selected from the camera roll goes into a UIImageView. This is the code I'm using:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
imageView.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
Now, I added in multiple UIImageViews, so that they can click "Add Image", and each time they select an image from the camera roll, the image will display in the next available UIImageView. This is where I'm having issues. I'm not sure how to do this, but this is the code that I've been trying to use to accomplish this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
if (imageView == nil) {
imageView.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
if (imageView2 == nil) {
imageView2.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
if (imageView3 == nil) {
imageView3.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
}
I have two issues here. The first issue is that this version of my code does not add the image at all like the 1st version of code I posted does. I suspect that its because the imageView's do not equal nil for some reason. I'm not sure how to fix that. The second issue is that if this code were to work, all of the UIImageViews will display the picture, rather than just the next available UIImageView. I tried putting "break;" after each if statement, but of course that caused an error because this is not a loop. Any help or advice is much appreciated, thank you!
Upvotes: 0
Views: 737
Reputation: 24481
The code you were running would set any null imageView's image to the selected image. Try this.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
if (imageView.image == nil) {
imageView.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
else if (imageView2.image == nil) {
imageView2.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
else if (imageView3.image == nil) {
imageView3.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
}
Upvotes: 1
Reputation: 1283
You are checking if the imageView
is nil
, not the image it contains. Also, you want to return from the function, not break a loop. Try this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
if (imageView.image == nil) {
imageView.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
return;
}
if (imageView2.image == nil) {
imageView2.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
return;
}
if (imageView3.image == nil) {
imageView3.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
return;
}
}
Upvotes: 2