views:

570

answers:

2

Hi,

I've got a very simple app where the user selects a UIImageView and presses a button to take a photo with the camera. The image is then returned and displayed in the UIImageView.

However, since the UIImageViews are sharing the same delegate, when there's already an image in one of the UIImageViews, and I go to take a photo to be placed in another, the previous UIImageView is replaced with empty content (i.e. no image). I guess this is because they're sharing the same delegate. Is there any way I can essentially copy the image instead of referencing the delegate version of it?

Here's some sample code:

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
[picker.parentViewController dismissModalViewControllerAnimated:YES];

if (topView == YES)
{
 NSLog(@"topView = %i", topView);
 imageView.image = image;
}
else {
 NSLog(@"topView = %i", topView);
 imageView2.image = image;
}

}

Thanks!

Edit: Here's the code that gets called by the IBAction on the button presses

- (IBAction) pushPick {
    topView = YES;
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}
- (IBAction) pushPick2 {
    topView = NO;
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}
A: 

use

[image copy];

But remember that you will need to release the object on dealloc

Chris Beeson
UIImage does not conform to NSCopying, so I wouldn't send the 'copy' message to it.
arul
+1  A: 

Something like this:

UIImage * newImage = [UIImage imageWithCGImage:[image CGImage]];
arul
Thanks, I'm sure this is right, but my app is still misbehaving. I've got two buttons that launch the imagepicker, one button for each UIImageView. I find that if I press the first button and choose an image, then the second and choose an image everything is fine, but pressing another afterwards assigns that image to the UIView, but clears out the other UIView. Any ideas?
mac_55
Hard to say, I'd try putting `NSLog(@"topView = %i", topView);` around the areas of code relying on that variable.
arul
Seems to be producing the correct results. Perhaps there's a better way to be tieing up multiple UIImageViews to UIImagePickers?btw everything seems to work ok if I choose a different UIImagePickerSourceType (e.g. UIImagePickerControllerSourceTypePhotoLibrary)
mac_55
Seems I'm getting memory warnings now, after the first image is assigned to the imageview. I think this explains what's going on, the images are being unloaded from the other image view. However, it doesn't explain why it's happening after only one photo being taken and assigned to an image view.
mac_55
Could you post the updated `- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo` method please ?
arul
I've edited the information above. I haven't changed this method much, I think it's just the log statements that have changed. Thanks!
mac_55
The Camera picker probably uses shared UIImage for storing the captured image. Try using `imageView.image = [UIImage imageWithCGImage:[image CGImage]];` instead in both cases.
arul
Thanks, I've changed those two lines. They don't help with the memory warnings though. I'm really baffled as to why these warnings are happening.
mac_55