Using UIImagePickerController in UIView - ios

I have button which when I click displays the camera (works perfectly)
- (IBAction)getPhoto:(id)sender
{
NSLog( #"Button clicked!" );
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
}
else { // IF the device doesn't have a camera, so use the photos album
[imagePicker setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
}
[self addSubview:imagePicker.view];
}
After which I can take an image, etc, - But when I click "use" : nothing happens here is my method : the Image picker does not dismiss itslelf : the application simply does nothing, I'm trying to take the image I just took and display it inside a CGRect on screen .
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:NULL];
UIImageView *patientImage = [[UIImageView alloc] init];
patientImage.frame = CGRectMake(625, 25, 83, 103);
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.patientImage.image = chosenImage;
}
Even when I hit the cancel button without taking an image, the imagePicker (camera interface) does not dismiss itself
Any help, advice, or guidance would be very appreciated.

You shouldn't be doing the "addSubview" thing up there but instead do:
[self presentViewController:imagePicker animated:YES completion:nil];

Instead of adding the picker as a subview, you should add the picker to a UIPopoverController.
You can then display it from within your UIView subclass by using the presentPopoverFromRect:inView:permittedArrowDirections:animated: popover method like this:
self.popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[self.popover presentPopoverFromRect:sender.frame inView:self permittedArrowDirections: UIPopoverArrowDirectionAny animated:YES];
Then, you can use the delegate methods to dismiss the popover using:
[self.popover dismissPopoverAnimatedYES];
The is required by the official docs:
The table indicates that on iPad, if you specify a source type of UIImagePickerControllerSourceTypePhotoLibrary or
UIImagePickerControllerSourceTypeSavedPhotosAlbum, you must present
the image picker using a popover controller, as described in
“Presenting and Dismissing the Popover” in UIPopoverController Class
Reference. If you attempt to present an image picker modally
(full-screen) for choosing among saved pictures and movies, the system
raises an exception.
On iPad, if you specify a source type of
UIImagePickerControllerSourceTypeCamera, you can present the image
picker modally (full-screen) or by using a popover. However, Apple
recommends that you present the camera interface only full-screen.

Is the getPhoto: method in the UIView subclass?
If thats the case, then it would be better to:
Move the getPhoto: code out of the UIView subclass entirely, and put it in the view's parent view controller.
Make your UIButton a property of your UIView: #property (nonatomic, strong) UIButton *myButton;
In your view controller, set the button's target & action as follows:
[myView.myButton addTarget:self action:#selector(getPhoto:) forControlEvents:UIControlEventTouchUpInside];
Finally, in your getPhoto: method, call [self presentViewController:imagePicker animated:YES completion:NULL];
Your - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info code should also go in the view controller, changing self.patientImage.image = chosenImage; to myView.patientImage.image = chosenImage;

Dont know why the cancel button is not working.
But make this change it will select the image you are clicking.
If you want an cancel event add it by creating custom UIBarButtonand add it.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImageView *patientImage = [[UIImageView alloc] init];
patientImage.frame = CGRectMake(625, 25, 83, 103);
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.patientImage.image = chosenImage;
[imagePicker.view removeFromSuperview];
}

Try:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker.view removeFromSuperview];
UIImageView *patientImage = [[UIImageView alloc] init];
patientImage.frame = CGRectMake(625, 25, 83, 103);
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.patientImage.image = chosenImage;
}

Remove the [self.addSubView:imagePicker.view]; and instead
[self presentViewController:imagePicker animated:YES completion:NULL];
You are dismissing the variable picker although picker is undefined. Instead put:
[self dismissModalViewControllerAnimated:YES completion:NULL];
and make sure its at the end of your method.

Related

Removing the cancel button from Custom camera

I am using a custom overlay frame, on camera. On the navigation bar I have app icon in the middle and a back button on left.
So I would like to remove the default cancel button besides the capture button but I do not want to remove camera click button. I have done research but I did not find a perfect answer. I have already used the following code but it also removes the click button.
[picker setShowsCameraControls:YES];
I feel you you should add custom CameraOverlayView to your uiimagepickercontroller and hide CameraControls.This won’t show your cancel button.
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[picker setShowsCameraControls:NO];
CGRect screenBounds= [UIScreen mainScreen].bounds ;
UIView *overlayView = [[UIView alloc] initWithFrame:screenBounds];
[overlayView setAutoresizesSubviews:YES];
[overlayView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin]; UIButton *captureButton=[[UIButton alloc]initWithFrame:CGRectMake(screenBounds.size.width/2-30, screenBounds.size.height-60,60 , 60)];
[captureButton addTarget:self action:#selector(captureImage:) forControlEvents:UIControlEventTouchUpInside];
[captureButton setBackgroundImage:[UIImage imageNamed:#"icon"] forState:UIControlStateNormal];
[overlayView addSubview:captureButton];
[picker setCameraOverlayView:overlayView];
[picker setCameraDevice:UIImagePickerControllerCameraDeviceRear];
[self presentViewController:picker animated:YES completion:NULL];
And this is action for Capture button.
-(void)captureImage:(UIButton*)sender
{
[picker takePicture];
}
And use this delegate method to get image.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
I think you disable but about removing i am not sure as I know it is not possible because UIImagePickerController inherits UINavigationController. Then you can get
UINavigationBar *bar = picker.navigationBar;
[bar setHidden:NO];
bar.navigationItem.rightBarButtonItem = doneButton;
It will work only for IOS 6
You can use this source ELCImagePickerController

UIIMagePickerController dismissViewControllerAnimated:completion: not working

I'm trying to pick an image from UIImagePickerController and then set it inline inside UITextView. The code below is pretty straightforward. When i tap "act" button, it opens up UIImagePickerController and once it's done, the delegate method didFinishPickingMediaWithInfo gets triggered.
- (IBAction)act:(id)sender{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
if(!self.imageView){
self.imageView = [[UIImageView alloc] initWithImage:chosenImage];
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];
[self.imageView setFrame:CGRectMake(10.0, 10.0, 100.0f, 100.0f)];
[self.content addSubview:self.imageView];
UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:CGRectMake(0.0,0.0,120.0f,110.0f)];
self.content.textContainer.exclusionPaths = #[exclusionPath];
} else {
[self.imageView setImage:chosenImage];
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
Here's the problem: when i try to dismiss UIImagePickerViewController using dismissViewController, it is just ignored. However, it works fine without the two lines where i'm setting the exclusionPaths. Also, this only happens when there's already text in the UITextView. If there's no text, I can do this as many times as I want and dismissViewController works fine. I've stepped through the code and the code actually does pass through the [picker dismissViewControllerAnimated: completion: line. Also picker at that point is indeed an instance of UIImagePickerController. Anyone have idea what is going on?
[UPDATE] I've actually trimmed down the code to the following:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:CGRectMake(0.0,0.0,120.0f,110.0f)];
self.content.textContainer.exclusionPaths = #[exclusionPath];
[picker dismissViewControllerAnimated:YES completion:nil];
}
Basically I don't even set the imageView anymore. It's just that when I try to set the exclusionPaths of a UITextView--self.content--the dismissViewControllerAnimated:completion: doesn't work. There is no error in the console.
You have to call dismissViewConroller: on self and not picker.
Better yet, call it on picker.presentingViewController like this:
[picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];

Image is not loading in view after selection from library with xcode 5

I am trying to simply select an image from the iphone’s library and display it on the view. Here is the header file reference:
#interface myappViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>{
UIImagePickerController *imagepicker;
UIImagePickerController *imagepicker2;
UIImage *image;
IBOutlet UIImageView *imagepickerview;
}
- (IBAction)TakePhoto;
- (IBAction)ChooseExisting;
#end
Here is the implementation code:
#interface myappViewController ()
#end
#implementation myappViewController
- (IBAction)TakePhoto{
imagepicker = [[UIImagePickerController alloc] init];
imagepicker.delegate = self;
[imagepicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:imagepicker animated:YES completion:NULL];
}
- (IBAction)ChooseExisting{
imagepicker2 = [[UIImagePickerController alloc] init];
imagepicker2.delegate = self;
[imagepicker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagepicker2 animated:YES completion:NULL];
}
- (void) imagePickerController:(UIImagePickerController *)imagepicker didFinishPickingMediaWithInfo:(NSDictionary *)info{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imagepickerview setImage:image];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)imagepickerview{
[self dismissViewControllerAnimated:YES completion:NULL];
}
On the storyboard I have two buttons. One for choose existing and one for take photo. If I select the choose existing the library opens and I can select an image. The problem is that the image is not selected and then redirected back to the xcode view. Rather, I have to click the cancel button in the navigation controller of the photo library and then I see the image loaded in the view. Any idea why this is not properly loading?
Please use valueForKey instead of objectForKey also dismiss the image picker before getting the image.
[self dismissViewControllerAnimated:NO completion:nil];
image = [info valueForKey:UIImagePickerControllerOriginalImage];
[imagepickerview setImage:image];

imageView doesn't show image from UIImagePickerController

I am trying to programmatically use an image from UIImagePickerController in one view controller and have that be the imageView in another view controller. Here is the method from viewController A
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *image = info[UIImagePickerControllerOriginalImage];
bandCropViewController *viewController = [[bandCropViewController alloc]init];
viewController.imageOriginal = image;
[self dismissViewControllerAnimated:YES completion:^{
// Move to Crop View Controller
[self presentViewController:viewController animated:YES completion:nil];
}];
}
ViewController B looks like this:
- (void)viewDidLoad
{
[super viewDidLoad];
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100, 320, 280)];
[self.imageView setImage:imageOriginal];
imageView.backgroundColor = [UIColor redColor];
[self.view addSubview:imageView];
}
viewController B is showing the imageView frame in red as it should be. But the image is not carrying over.
Any thoughts as to what I am doing wrong?
Highly recommend this tutorial
It should help you out. It looks like you are possibly doing some unnecessary things in
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

ImagePicker in a popover does not hide

I have read that the iPad must use a UIPopoverController to view the PhotoLibrary, however, I have edited the code to make it, the popover shows but it does not hide when I choose a picture.
I found that it does not reach the didFinishpickingMediaWithInfo. Am I missing anything? here is my code
-(IBAction) ButtonClicked{
ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
ipc.delegate=self;
popover = [[UIPopoverController alloc] initWithContentViewController:ipc];
[ipc release];
[popover presentPopoverFromRect:CGRectMake(0.0, 0.0, 800.0, 400.0)
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
here:
-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
[picker release];
}
and here:
-(void) imagePickerController:(UIImagePickerController *)picker didFinishpickingMediaWithInfo:(NSDictionary *)info{
// TempImage is a UIImage instance
TempImg = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//bgImage is a UIImageView instance and it's connected in the IB
[bgImage setImage:TempImg];
// Dismiss UIImagePickerController and release it
[picker dismissModalViewControllerAnimated:YES];
[picker.view removeFromSuperview];
[picker release];
}
I really need someone's help, I have already watched every youtube video, read every article on the internet and tried almost everything. I would really appreciate your help.
The first problem is that the method didFinishpickingMediaWithInfo is spelled wrong and so it won't get called. It should be didFinishPickingMediaWithInfo (uppercase P for Picking).
Second problem is calling dismiss on the parent or the picker will not hide the popover. Instead, try calling [popover dismissPopoverAnimated:YES];.

Resources