Perform "Use Photo" Button on Custom Image Picker Overlay

How to get the custom overlay for UIImagePicker camera to be full screen in iOS 7?

After many attempts, this is what worked for me with many thanks to other people's suggestions. The following facts were very helpful to know and keep in mind:

The camera's points resolution is 426 * 320. In order for the camera preview's height to be stretched to the phone's screen height of 568, it needs to be multiplied by a factor of 1.3333 when using CGAffineTransformScale.

Note that the below are hard coded with various numbers based on the iPhone 5's screen resolution in points. They could be improved by using such objects such as screen.height, screen.width and other variables to make it applicable to iPhone 4/4s dimensions as well.

    self.imagePickerController.showsCameraControls = NO;

[[NSBundle mainBundle] loadNibNamed:@"overlayView" owner:self options:nil];
self.overlayView.frame = self.imagePickerController.cameraOverlayView.frame;
self.imagePickerController.cameraOverlayView = self.overlayView;
self.overlayView = nil;

//For iphone 5+
//Camera is 426 * 320. Screen height is 568. Multiply by 1.333 in 5 inch to fill vertical
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 71.0); //This slots the preview exactly in the middle of the screen by moving it down 71 points
self.imagePickerController.cameraViewTransform = translate;

CGAffineTransform scale = CGAffineTransformScale(translate, 1.333333, 1.333333);
self.imagePickerController.cameraViewTransform = scale;

Making an Image Picker with UIButton in Swift

UIImagePickerController can't be added in storyboard, so you must do that programmatically. First, create an IBAction with your UIButton:

- (IBAction)openImagePicker:(id)sender;

Your class need to adopt UIImagePickerControllerDelegate and UINavigationControllerDelegate protocol:

@interface MyViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

then show image picker in openImagePicker: method:

// Check if image source is available
// In this case, photo library is always available. If you use
// UIImagePickerControllerSourceTypeCamera instead, simulator doesn't have it
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

UIImagePickerController *picker = [[UIImagePickerController alloc] init]; // Create an image picker
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // Choose source type
picker.delegate = self; // Delegate when image is picked
picker.allowsEditing = YES; // Allow user to edit image before choose it
[self presentViewController:picker animated:YES completion:nil]; // Show image picker
}

To use the picked image, you must implement this method of UIImagePickerControllerDelegate:

/**
* @param picker The image picker
* @param info Dictionary contains picked images and metadata...
*/
- (void)imagePickerController:(nonnull UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(nonnull NSDictionary *)info {
// Because we allow user to edit image,
// so we choose UIImagePickerControllerEditedImage
// You can see other keys in UIImagePickerController class header
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];

// Dismiss the picker
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:nil];
});

// Use the image here
}

UIImagePickerController Overlay Buttons Not Firing

The problem is this line:

self.overlayView.frame = self.cameraOverlayView.frame;

Our cameraOverlayView is nil; it has no frame, because it doesn't even exist yet. So we are giving the overlayView a zero frame. It is dimensionless. The button appears, but it has a zero-sized superview and therefore cannot be tapped.

Now, you may ask, why is that? It's because of the way touch delivery works in iOS. To be touchable, a subview's superview must also be touchable, because the superview is hit-tested for the touch before the subview. But a zero-size view is not touchable: your touch misses the overlayView, so it misses the button as well.

The solution might be as simple as giving the overlayView a reasonable real frame. There may also be timing problems about when you do that (e.g. it may be that you cannot really set the frame until the image picker is about to appear - viewWillAppear:), but in any case this is the place to start.

Custom Use, Cancel,“Retake” and Reverse Camera button event using UIImagePickerController

You can build a UIViewController, take it's view as the image picker's overlayView.
All your custom buttons are controlled by this UIViewController.

e.g.

UIButton *myBtn = [[UIButton alloc] initWithFrame:btnFrame];

[myBtn addTarget:self action:@selector(myBtnPressed) forControlEvents:UIControlEventTouchUpInside];

[overlayView addSubview:myBtn];


Related Topics



Leave a reply



Submit