Can't pop UIImagePickerController - ios

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

Related

How to overlay camera capture image?

I am replacing the part of image by capture image from camera. i need to perform the activity like this app in my application. https://itunes.apple.com/in/app/replace-my-face-funny-girls/id794033892?mt=8
I need a way of how can i take picture from background view with super view image and save the new generated image with merging of both image.
Please suggest me solution.
The simple work i did
- (void)viewDidLoad {
[super viewDidLoad];
self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.delegate = self;
self.picker.allowsEditing = NO;
self.picker.showsCameraControls = NO;
self.picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
CGRect overlayRect = CGRectMake(0, 0, self.imageView.frame.size.width,self.imageView.frame.size.height);
UIView *overlayView = [[UIView alloc] initWithFrame:overlayRect];
UIImageView *capture_image=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.imageView.frame.size.width,self.imageView.frame.size.height)];
capture_image.image=[UIImage imageNamed:#"1.jpg"];
[overlayView addSubview:capture_image];
[self.picker setCameraOverlayView:overlayView];
[self.navigationController presentViewController:self.picker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Please also suggest me if there is any repository working like this approach.
About capturing the image: you might want to look at FastttCamera, which allows you to set the camera view to your defined image view and crops the image accordingly.
Good luck!

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

change text color of UIBarButtonItem in UIImagePickerController?

I use native UIImagePickerController in of my view controller.
I use a different color for UIBarButtonItem than what iPhone uses UIImagePickerController
is it possible for me to change textColor of UIImagePickerController.UIBarButtonItem ??
I tried
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.navigationItem.rightBarButtonItem.tintColor = [UIColor redColor];
for (UIBarButtonItem *item in picker.navigationItem.rightBarButtonItems) {
item.tintColor = [UIColor redColor];
}
for (UIView *view in picker.navigationBar.subviews) {
NSLog(#"%#", [[view class] description]);
if ([[[view class] description] isEqualToString:#"UIBarButtonItem"]) {
[(UIBarButtonItem *)view setTintColor:[UIColor redColor]];
}
}
[self presentViewController:picker animated:YES completion:NULL];
Output of NSlog was
_UINavigationBarBackground
_UINavigationBarBackIndicatorView
EDIT 2:
I needed to do
picker.navigationBar.tintColor = [UIColor redColor];

Hide ActionSheet iOS

My application is about clicking on a button with background image named "uploadphoto1.png". When I click on the button, it shows me an ActionSheet letting me choose to take a picture with the camera or taking a picture from the library.
When, for example, I choose a picture from the camera, it lets me select a picture and show the picture in an imageView and it change the background image of the button from upload.png to save.png.
The problem now that when I click on the button with background Image "save1.png" it shows me the actionSheet.
I want to hide the actionSheet.
Here is the code:
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *buttonpicture =[UIImage imageNamed:#"uploadphoto1.png"];
[ButtonPicture setBackgroundImage:buttonpicture forState:UIControlStateNormal];
[ButtonPicture setFrame:CGRectMake(173, 269, 140, 30)];
}
- (IBAction)ButtonPicture:(UIButton *)sender
{
NSLog(#"Button Picture Pressed");
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:#"Photo" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Picture From Camera",#"Picture From Gallery",nil];
[actionSheet setTag:1001];
[actionSheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex)
{
case 0:
{
// Take a picture from camera
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
}
break;
case 1:
{
// take a picture from gallery photos
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self.view addSubview:toolbar];
[self presentModalViewController:picker animated:YES];
}
break;
default:
break; }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[ButtonPicture setBackgroundImage:[UIImage imageNamed:#"save1.png"] forState:UIControlStateNormal];
[picker dismissModalViewControllerAnimated:YES];
imageview.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//[actionSheet showInView:Nil];
}
#interface YourViewController
{
BOOL imageChanged;
}
- (void)viewDidLoad
{
[super viewDidLoad];
imageChanged = NO;
UIImage *buttonpicture =[UIImage imageNamed:#"uploadphoto1.png"];
[ButtonPicture setBackgroundImage:buttonpicture forState:UIControlStateNormal];
[ButtonPicture setFrame:CGRectMake(173, 269, 140, 30)];
}
- (IBAction)ButtonPicture:(UIButton *)sender
{
NSLog(#"Button Picture Pressed");
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:#"Photo" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Picture From Camera",#"Picture From Gallery",nil];
[actionSheet setTag:1001];
if (!imageChanged)
[actionSheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex)
{
case 0:
// Take a picture from camera
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
break;
case 1:
// take a picture from gallery photos
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self.view addSubview:toolbar];
[self presentModalViewController:picker animated:YES];
break;
default:
break;
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[ButtonPicture setBackgroundImage:[UIImage imageNamed:#"save1.png"] forState:UIControlStateNormal];
imageChanged = YES;
[picker dismissModalViewControllerAnimated:YES];
imageview.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
There are various ways this could be handled. One way is to set a boolean flag when you don't want tapping on the button to bring up the actionSheet. Then, in - (IBAction)ButtonPicture:(UIButton *)sender you can check the boolean flag to decide whether or not to create the actionSheet and show it.

How to add UIImagePickerViewController as a subview in ipad?

How to add UIImagePickerViewController as a subview in ipad.
in iphone i used like this this working fine
self.pick = [[UIImagePickerController alloc]init];
pick.delegate = self;
pick.sourceType = UIImagePickerControllerSourceTypeCamera;
pick.sourceType = UIImagePickerControllerCameraDeviceFront;
pick.showsCameraControls = NO;
CGRect rect = CGRectMake(0, 0, 200, 250);
pick.view.frame = rect;
UIView *cameraView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
[cameraView setBackgroundColor:[UIColor greenColor]];
[cameraView addSubview:pick.view];
[self.view addSubview:cameraView];
[pick viewWillAppear:YES];
[pick viewDidAppear:YES];
[pick release];
but same way i pad is not working. ipad is getting full view when camera is open.
i don't want to use popoverview controller.
pls help me?
UIImagePickerController must be presented with UIPopoverController on iPad.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:picker];
[popover presentPopoverFromRect:self.selectedImageView.bounds inView:self.selectedImageView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
self.popOver = popover;
} else {
[self presentModalViewController:picker animated:YES];
}

Resources