Removing the cancel button from Custom camera - ios

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

Related

Can't pop UIImagePickerController

I have a UIImagePickerController with a custom overlay. I am trying to implement a cancel button that will just pop the UIImagePickerController. My code for the setup is below:
- (void)tapPhoto {
//setup the picker
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.cameraDevice=UIImagePickerControllerCameraDeviceFront;
self.picker.showsCameraControls=NO;
self.cameraOverlay = [[CameraOverlay alloc] init];
UIImage *camoverlayImage = self.cameraOverlay.cameraOverlayImage;
UIImageView *camoverlayImageView = [[UIImageView alloc] initWithImage:camoverlayImage];
camoverlayImageView.frame=CGRectMake(0, 0, self.cameraOverlay.previewSize.width , self.cameraOverlay.previewSize.height);
//init the control view
UIView *cameraControls= [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
[cameraControls addSubview:camoverlayImageView];
//Shutter Button
UIButton *button = [[UIButton alloc] init];//70*70
[button setBackgroundImage:[UIImage imageNamed:#"cameraoff"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"cameraon"] forState:UIControlStateHighlighted];
button.frame = CGRectMake(([[UIScreen mainScreen] bounds].size.width/2)-70/2, [[UIScreen mainScreen] bounds].size.height-70-10, 70 , 70);
[button addTarget:self action:#selector(takePhoto) forControlEvents:UIControlEventTouchUpInside];
[cameraControls addSubview:button];
UIButton *button2 = [[UIButton alloc] init];
[button2 setTitle:#"Cancel" forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(cancelPhoto) forControlEvents:UIControlEventTouchUpInside];
button2.frame = CGRectMake(5, [[UIScreen mainScreen]bounds].size.height-30, 0, 0);
[button2 sizeToFit];
[cameraControls addSubview:button2];
[self.picker setCameraOverlayView:cameraControls];
[self presentViewController:self.picker animated:YES completion:NULL];
}
-(void)cancelPhoto{
NSLog(#"photo canceled");
[self.navigationController pushViewController:self.picker animated:YES];
}
-(void)takePhoto{
//this will auto trigger the control
[self.picker takePicture];
}
I am trying to pop it from the navigation controller but not having any luck. I have also tried [self.picker popViewControllerAnimated:YES];. Could anyone give me any pointers on why this isn't working? Thanks
You are (correctly) presenting your picker:
[self presentViewController:self.picker animated:YES completion:NULL];
The opposite of present is not push or pop, but dismiss:
[self dismissViewControllerAnimated:YES completion:nil];
First this line is wrong!
[self.navigationController pushViewController:self.picker animated:YES];
Why you are pushing the picker, when your picker is already presented in your current view controller
The correct method is this:-
-(void)cancelPhoto{
//as you have presented the view controller so have dismiss it by this way.
[self.picker dismissViewControllerAnimated:YES completion:nil];
}
Try like this-
-(void)tapPhoto {
//set up the Picker
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.cameraDevice=UIImagePickerControllerCameraDeviceFront;
//add a UIView as CameraOverLay
UIView *overlayView = [[UIView alloc] initWithFrame:picker.view.frame];
[self.picker setCameraOverlayView:overlayView];
//Present the Picker
[self presentViewController:picker animated:YES completion:nil];
}
This way, you should have a cancel button by default. Then just implement the delegate method and dismiss the controller when you hit on the cancel button.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//get the captured image if any
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
== kCFCompareEqualTo){
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//save the image in photo library
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
//pop out or remove the picker
[picker dismissViewControllerAnimated:YES completion:nil];
}
Don't try to use popViewControllerAnimated. Instead try:
[self.picker dismissViewControllerAnimated:YES completion:nil];

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

UIPickerController crashes the app

I have a UIPickerController that gets your pictures and allows you to pick some of them though at the moment when I click the button to activate it the app crashes.
Here is the code that I am using for it:
in my ViewDidLoad method:
pickerController = [[UIImagePickerController alloc] init];
pickerController.allowsEditing = NO;
pickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
The function:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:nil];
patientPicture = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
UIImageView *pictureView = (UIImageView *)[imageCell viewWithTag:777];
pictureView.image = patientPicture;
[_imgViewAdd reloadInputViews];
}
And it being called:
- (IBAction)addPicture:(id)sender {
[self presentViewController:pickerController animated:YES completion:nil];
}
It is wierd because I have recently changed my app to Ipad only though while it was in IPhone it worked fine. When you click the button in NSLog this error message crops up which I supose is something to do with it:
UIStatusBarStyleBlackTranslucent is not available on this device.
I suspect this is quite a common issue that people have
Thanks in advance
Try presenting in a popover...
pickerController = [[UIImagePickerController alloc] init];
UIPopoverController *popOverController = [[UIPopoverController alloc] initWithContentViewController:pickerController];
popOverController.delegate = self;
and to present...
[popOverController presentPopoverFromRect:yourframe inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

Using UIImagePickerController in UIView

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.

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