Going to a different view once photo selected from camera/library - iphone

I am writing an app that has an initial view containing 2 buttons - one that allows the user to take a photo with the camera and the other that allows him to select a picture from the library.
I've written the code that allows that to happen, but after selecting the picture, I want to go to another view that allows, say, sharing the picture or whatever. Could anyone tell me how to do something like "whenPhotoIsSelected, view = newView"?
This is my code so far:
#pragma mark -
-(IBAction) getCameraPicture: (id) sender{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsImageEditing = YES;
picker.sourceType = (sender == takePictureButton) ?
UIImagePickerControllerSourceTypeCamera :
UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
[picker release];
}
I am aware of the existence of
-(void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
but how exactly do I use it? I tried it and it crashes...

Implement the UIImagePickerControllerDelegate in your ViewController.
To do that add <UIImagePickerControllerDelegate> to your ViewController's interface declaration (in the .h file) like so:
#interface YourViewController : UIViewController <UIImagePickerControllerDelegate> {
// instance variable declarations etc.
}
Then in your ViewController's .m file you actually implement the method -(void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo instead of calling it, like so:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
// add the new view, for example you could push it on your navigation stack if you use a UINavigationController
}
That method will be called by the UIImagePicker when an image was selected.

Related

how to call imagepickerdelegate method two times when picking images from dirrerent button

I'm relatively new to X Code
and
I m working on photo collage App , when i pick image from one picker then image picker is working properly but i want to pick different images from different imagepicker then image picker is not working properly
Anyone help me solve my problem. Here is my code
`
-(IBAction)imagepickMethod1:(id)sender
{
imagepicker=[[UIImagePickerController alloc]init];
imagepicker.delegate=self;
imagepicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagepicker animated:YES];
button1.tag=100;
}
-(IBAction)imagepickMethod2:(id)sender
{
imagepicker1=[[UIImagePickerController alloc]init];
imagepicker1.delegate=self;
imagepicker1.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagepicker1 animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
[picker dismissModalViewControllerAnimated:YES];
imagepicker.view.hidden=YES;
photoPreviewImageView.image=image;
}
-(void)imagePickerController1:(UIImagePickerController *)picker1 didFinishPickingImage:(UIImage *)image1 editingInfo:(NSDictionary *)editingInfo1
{
[picker1 dismissModalViewControllerAnimated:YES];
imagepicker.view.hidden=YES;
photoPreviewImageView1.image=image1;
}
`
//Take two imageView in your .h file
UIImageView *imgViewForFirstPicker;
UIImageView *imgViewForSecondPicker;
// Alloc these images in view did load
imgViewForFirstPicker = [[UIImaeView allo] initWithFrame:(give your rect)];
// Similarly for second imageView and add to both in self.view
-(IBAction)imagepickMethod1:(id)sender
{
UIImagePickerController *imagepicker=[[UIImagePickerController alloc]init];
imagepicker.delegate=self;
imagepicker.tag=100;
imagepicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagepicker animated:YES];
}
-(IBAction)imagepickMethod2:(id)sender
{
UIImagePickerController *imagepicker1=[[UIImagePickerController alloc]init];
imagepicker1.delegate=self;
imagepicker1.tag=101;
imagepicker1.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagepicker1 animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage: (UIImage*)image editingInfo:(NSDictionary *)editingInfo
{
[picker dismissModalViewControllerAnimated:YES];
if(picker.tag == 100)
imgViewForFirstPicker.image=image;
else
imgViewForSecondPicker.image=image;
}
try this one hope it will help you
UIImagePickerController creates a pretty heavy object, So I don't think it is advisable to create multiple instances of it. May be this is what is causing the problem if you are doing the same.. It would be nice if you could share your code as well so we can have more insights into your problem
It is advisable to just use on image picker. From the code you provided you are trying to create two different imagepicker delegate methods, but in reality only one of them will be called both times.
You should create on instance of imagePicker and change its tag depending on which image you needed changed and then in the -didFinishPickingImage check if (picker.tag == SOME_TAG) then set appropriately.

How to share UIImagepickercontroller code for 2 buttons

In my project I am using the UIIamgepickercontroller to select an image from the library and load it into a UIImageView. I am doing this for 2 images so I have two buttons for each image view, but I do not want to replicate the code for image picker twice and I'm not sure how to implement so that the method knows which image view to load the image into. I think I need to use button tags? but can't find the right method.
here's my code:
.h
`#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#interface LoadViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
IBOutlet UIImageView *imageView;
IBOutlet UIImageView *imageView2;
}
- (IBAction)pick1;
- (IBAction)pick2;
- (void) getImage;
#end`
.m
#import "LoadViewController.h"
#implementation LoadViewController
UIImage *imageHandle;
- (IBAction)pick2 {
[self getImage];
imageView2.image = imageHandle;
}
- (IBAction)pick1{
[self getImage];
imageView.image = imageHandle;
}
- (void)getImage {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
imageHandle = image;
[picker.parentViewController dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker.parentViewController dismissModalViewControllerAnimated:YES];
}
#end
Another problem I'v been having is that the methods to catch whether I have selected an image or cancelled the view do not seem to work although if I comment out the entire method (void)imagePickerControllerDidCancel then it will cancel?!?
I'm in the early stages of learning this stuff and any help would be very much appreciated!
Thanks
You can identify the button with a tag (you can set this in Interface Builder), which is an arbitrary integer. The problem is that you've rejected the chance to receive a reference to the sender (the button); instead of - (IBAction)pick1, say - (IBAction)pick1:(id)sender. Now you can check the sender's tag (cast the sender to a UIView* so the compiler understands what you're doing).

UIImagePicker always issues imagePickerControllerDidCancel in iOS 5

So I'm retesting one of my apps that I know works with iOS 4.2 and up on the new iOS 5, and for some reason, they don't want to play nice. My UIImagePicker delegate will, upon the user selecting a photo from the photo library, ALWAYS issue an imagePickerControllerDidCancel, and never allow the picture to be selected (just says the image is nil when I try to load it). This is the code I know works on previous versions of iOS, so I have no idea why it wouldn't work now, unless Apple has changed how UIImagePicker works.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage : (UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
mainPhoto.image = image;
[self saveImage:image];
[picker dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
[picker dismissModalViewControllerAnimated:YES];
}
And here is the code that calls the UIImagePicker:
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
[picker release];
Any ideas? Or has anyone else had this problem?
Documentation for
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage : (UIImage *)image editingInfo:(NSDictionary *)editingInfo says,
"Deprecated in iOS 3.0. Use imagePickerController:didFinishPickingMediaWithInfo: instead.".Try to follow the suggestion and let us know if you still face issue ?

Capturing image using custom buttons in cameraoverlayview in iphone

Thanks in advance.
I have created a custom toolbar on cameraOverlayView. One of the toolbar buttons will trigger the takeSnap method –
-(IBAction)takeSnap
{
NSLog(#"take snap called");
[self.picker takePicture];
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editingInfo
{
UIImage *image = img;
NSLog(#"image : %#",image);
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
NSLog(#"Did cancel called");
}
But it is not calling the delegate methods after capturing. The view continues to remain in capture mode. If any one know the reason please help me.
Its very likely that you haven't assigned a delegate properly.

UIImagePicker doesn't showing selected image

I'm new to iPhone and I'm using this code to select an image from the iPhone library and show it in imageView. The library is showing images, but when I'm selecting the image, it doesn't appear in the image view..
- (void)viewDidLoad {
[super viewDidLoad];
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
Please can you tell me what's the problem with the code?
Try with:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
imageView.image = pickedImage;
[self dismissModalViewControllerAnimated:YES];
}
UIImagePickerControllerOriginalImage is a string constant that could be different from #"UIImagePickerControllerOriginalImage" string.
Then, it's safe and more clear to former set the image view, and latter to dismiss the modal view controller.
Pay attention that you should invoke dismissModalViewControllerAnimated: on self because you are dismissing the modal view controller "owned" by self.
Bye!
I fixed my issue of "grey box appearing instead of an image in UIImageView".
I forgot to make the Background color of the UIIMageView to be CLEAR COLOR. THe problem was solved by setting background color to clearColor.
Was using XIB and you should also check if anything is laid out wrong in Interface builder in case you are using interface builder to create UI.