How to get a copy of the image from UIImagePicker - iphone

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

Something like this:
UIImage * newImage = [UIImage imageWithCGImage:[image CGImage]];

use
[image copy];
But remember that you will need to release the object on dealloc

Related

How to get Sender Button Tag/Name/ID in UIImagePickerController - didFinishPickingMediaWithInfo?

Can somebody guide me on getting the sender button Tag/Name/ID in below method?
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
// I Want to get the sender button reference here
}
you can use picker.view.tag to identify the UIImagePickerController type. whenever you called UIImagePicker you can set the tag of its view.
I have used the same for identifying different image picker.
EDIT
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.allowsEditing = YES;
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.view.tag = 5;//set the tag here
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editingInfo
{
NSLog(#"%d",picker.view.tag);
}
you will get the tag value wahtever you will set.
Try this
-(IBAction)ownMethod:(id)sender
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.allowsEditing = YES;
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
**imagePickerController.view.tag = [sender tag];//set the tag of button to picker tag **
[self presentViewController:imagePickerController animated:YES completion:nil];
}

How to use move and scale UIImagePickerController in iPhone?

I am new to iPhone development. I am working with image picker controller in which I want to move and scale the image that picked from the photo library, so I enabled the allowsediting = yes.
And my picker did finish method:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
self.imgForDisplay.image=[self scaleAndRotateImage:image];
self.imagePicker.allowsEditing = YES;
ImageDetailsViewController *imageDetailsVC = [[ImageDetailsViewController alloc] init];
imageDetailsVC.imagedisplay.image = self.imgForDisplay.image;
[picker pushViewController:imageDetailsVC animated:YES];
//[picker dismissModalViewControllerAnimated:YES];
}
and I push another view controller where I want to show the Image picked.
Here I have two problems:
1) When I select the image, its navigating to another view without "move and scaling" screen but the image is now displayed in the ImageDetailsViewController when it navigates.
2) When I again navigate back and pick another image, now it shows the "move and scaling" screen. When I click the choose button, it is simply showing a white screen with out navigation bar.
Is there any way to set the action for the choose button?
I too face the same problem i solved it by using the by using the code, go through this it will be usefull.
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
self.lastChosenMediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([lastChosenMediaType isEqual:(NSString *)kUTTypeImage]) {
UIImage *chosenImage = [info objectForKey:UIImagePickerControllerEditedImage];
UIImage *shrunkenImage = shrinkImage(chosenImage, imageFrame.size);
self.image = shrunkenImage;
} else if ([lastChosenMediaType isEqual:(NSString *)kUTTypeMovie]) {
self.movieURL = [info objectForKey:UIImagePickerControllerMediaURL];
}
ImageSelectionView *imageSelectionVC = [[ImageSelectionView alloc] init];
imageSelectionVC.theImage = image;
[picker pushViewController:imageSelectionVC animated:YES];
imageSelectionVC.dismissDelegate =self;
// [picker dismissModalViewControllerAnimated:YES];
}
'
-(void)updateDisplay {
if ([lastChosenMediaType isEqual:(NSString *)kUTTypeImage]) {
imageView.image = image;
imageView.hidden = NO;
moviePlayerController.view.hidden = YES;
} else if ([lastChosenMediaType isEqual:(NSString *)kUTTypeMovie]) {
[self.moviePlayerController.view removeFromSuperview];
self.moviePlayerController = [[MPMoviePlayerController alloc]
initWithContentURL:movieURL] ;
moviePlayerController.view.frame = imageFrame;
moviePlayerController.view.clipsToBounds = YES;
[self.view addSubview:moviePlayerController.view];
imageView.hidden = YES;
}
}
I think you would be better off presenting your picker modal like this:
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = YES;
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
Then have your image displayed in the view of the viewcontoller that presented it.
-(IBAction)Btn_AddImage:(id)sender
{
UIImagePickerController *Image_Picker=[[UIImagePickerController alloc]init];
Image_Picker.delegate=self;
Image_Picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:Image_Picker animated:YES completion:Nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSData *Data=UIImageJPEGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"], 1);
UIImage *Cust_Image=[[UIImage alloc]initWithData:Data];
Profile_Image.image=Cust_Image;
[picker dismissViewControllerAnimated:YES completion:Nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:Nil];
}

Set an image from camera roll to UIImageView

I've implemented a PickerController to have a user select an image, but I want the image the user selects to be displayed on the UIImageView I have.
I use the following to go to the camera roll:
- (void) makeUIImagePickerControllerForCamera:(BOOL)camera {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[picker setMediaTypes:[NSArray arrayWithObjects:(NSString *) kUTTypeImage, nil]];
[self presentModalViewController: picker animated: YES];
[self presentModalViewController: picker animated: YES];
}
Which brings up the camera roll in which a user can click on a photo. However, after the click, it returns to the main view, which is obvious... But how can I assign an action to the button, or image the user selects. Something like UIOKButton?
Something like:
if ([UIImagePickerController presentModalViewController:nil image:nil] == UIOKButton ) {
NSArray *images = [UIImagePickerController images];
for(UIImage* pickerImage in [UIImagePickerController images])
{
myUIImage.image setImage pickerImage;
//^in the view
}
I'm not exactly sure how to go about doing this.
All responses are appreciated.
Thanks!
- (void) makeUIImagePickerControllerForCamera:(BOOL)camera {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[picker setMediaTypes:[NSArray arrayWithObjects:(NSString *) kUTTypeImage, nil]];
[self presentModalViewController: picker animated: YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// Dismiss the picker
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
// Get the image from the result
UIImage* image = [info valueForKey:#"UIImagePickerControllerOriginalImage"];
myUIImage.image = image;
}
Use the delegate method: – imagePickerController:didFinishPickingMediaWithInfo:.

captured image not showing in uiimageView

I have a uibutton to trigger an iphone's camera to grab an image, and I have a uiimageView in same view to show captured image, but it not showing anything after capturing.
Any solution?
here's my code:
(IBAction)grabImage:(id)sender{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsImageEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController: picker animated:YES];
[picker release];
}
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage : (UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
cameraImageView.image = image;
[picker dismissModalViewControllerAnimated:YES];
}

Re-using the UIImagePickerController.cameraOverlayView

I have a hierarchy of views:
compView with two children:
bgView
objectView.
I'm passing objectView to the cameraOverlayView property of the UIImagePickerController. Once the ImagePicker is done I update the image in the bgView and I would like to continue to display the view tree including the objectView which was passed to the UIImagePickerController as an overlayView.
The problem is that after calling dismissModalViewControllerAnimated to close the UIImagePickerController, the objectView is not appearing, as if the UIImagePickerContoller is hiding it. I checked in the debugger and it doesn't seem to be dealocated. The pointer is still valid. But the view is not showing.
Any ideas of what I might be doing wrong?
Thanks,
Eddy
Here a bit of code to show you what I'm doing:
From myViewController.m:
- (void)loadView {
CGRect rect = self.navigationController.view.bounds;
compView = [[UIView alloc] initWithFrame:rect];
bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Storage/dummy_bg.png"]];
objectView = [[PropImageView alloc] initWithImage:[UIImage imageNamed:#"Storage/rasta.png"]];
[objectView setUserInteractionEnabled:YES];
[objectView setMultipleTouchEnabled:YES];
[compView addSubview:bgView];
[compView addSubview:objectView];
self.view = compView;
[self placeObject];
[objectView release];
[bgView release];
[compView release];
}
- (void) placeObject
{
NSLog(#"camera button pressed");
// make sure camera is avaialble before setting it as source
//
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
#if !TARGET_IPHONE_SIMULATOR
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
picker.allowsEditing = NO;
picker.view.hidden = NO;
picker.cameraOverlayView = objectView;
[self presentModalViewController:picker animated:NO];
[picker release];
#endif
}
// from PickImageAppDelegate.m
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
// Dismiss the image selection, hide the picker and
[picker dismissModalViewControllerAnimated:YES];
bgView.image = [self rotateImage:image byOrientationFlag:UIImageOrientationRight];
}
[picker dismissModalViewControllerAnimated:YES];
That should be
[self dismissModalViewControllerAnimated:YES];