Is there a way to add a UIImagePickerController as the subview of a UIView, instead of presenting it modally?
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate=self;
[picker setSourceType:(UIImagePickerControllerSourceTypeCamera)];
[self presentViewController:picker animated:YES completion:Nil];
Related
In my app, the frame size of my UIDocumentPickerViewController always full screen as the following picture and the following code. However, I find there is a different frame size for the UIDocumentPickerViewController but I don't know how to get the effect as the second picture. Does anyone know how to get the effect?
https://drive.google.com/file/d/1_q_kfIIYA0d0KcT7NyPpFb6Q1vymU-lb/view?usp=sharing
- (void)btnPressDown {
NSLog(#"Press Down QQ");
[self showDocumentPickerInMode:UIDocumentPickerModeImport];
}
- (void)showDocumentPickerInMode:(UIDocumentPickerMode)mode {
NSLog(#"showDocumentPickerInMode QQ");
UIDocumentPickerViewController *picker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:#[#"public.data"] inMode:mode];
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
https://drive.google.com/file/d/177W-O0eJmePhLfS8ifGlw9n3jj26FDn9/view?usp=sharing
You need to set presentation style. The presentation style determines how a modally presented view controller is displayed onscreen.
"picker.modalPresentationStyle = UIModalPresentationFormSheet;"
- (void)showDocumentPickerInMode:(UIDocumentPickerMode)mode {
UIDocumentPickerViewController *picker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:#[#"public.data"] inMode:mode];
picker.modalPresentationStyle = UIModalPresentationFormSheet;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
Thats it :)
If I present a UIImagePickerController on a childViewController like this:
-(void)presentImagePicker{
UIViewController *childVC = [[UIViewController alloc] init];
[self addChildViewController:childVC];
ImagePickerController *imagePicker = [[ImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
[childVC presentViewController:imagePicker animated:YES completion:nil];
}
When I dismiss the childView:
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
NSArray *childVCArray = [self childViewControllers];
[[childVCArray firstObject] dismissViewControllerAnimated:YES completion:nil];
}
the rootViewController is re-initialized.
How can I prevent this behavior?
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init] ;
[imagePicker setAllowsEditing:YES] ;
[imagePicker setDelegate:self] ;
[imagePicker setMediaTypes:[NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil]] ;
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary] ;
[self presentViewController:imagePicker animated:YES completion:nil] ;
Step 1: Add Delegate
#interface APPViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
Step 2: Initialize UIImagePicker on click button Action
- (IBAction)selectPhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
Step 3:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Step 4: Dismiss controller
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Note: Post your Question with detail description
You are using UIImagePickerController as a variable not as an attribute or property, it means that it will be released at the end of your function. Set your UIImagePickerController as an attribute or property and then the delegate will work.
If you are using a UIPopoverPresentationController, make sure you dismiss the popover before presenting the imagePicker. Otherwise, it will not be shown.
I created a CameraOverlayViewController with .xib file to implement a customized camera overlayView with. The overlayView is just a transparent background with a button to select photo from the photoLibrary and it loads successfully in the mainViewController with the following code:
CameraOverlayViewController* overlay = [[CameraOverlayViewController alloc] initWithNibName:#"CameraOverlayViewController" bundle:nil];
cameraPicker.cameraOverlayView = overlay.view;
However, nothing happens when I pressed the button on the cameraOverlayView. No error messages, nothing. Does anyone know what's going on here? Below is my code in the CameraOverlayViewController file:
- (IBAction)select:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
[self dismissViewControllerAnimated:YES completion:^{
[self presentViewController:picker
animated:YES completion:nil];
}];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
NSLog(#"Just work alrdy!!!");
}
Thanks in advance!
Try This..
-(IBAction)select:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self dismissViewControllerAnimated:YES completion:^{
[self presentViewController:picker
animated:YES completion:nil]; ////////check this is called??????
}];
}
I have two views. Very simple set up just to play around with the camera roll.
One view will bring up the camera roll using:
[self presentViewController:self.imagePicker animated:NO completion:nil];
which is triggered by a UIButton. It brings up the camera, everything's fine.
After taking a picture, I am calling the following to dismiss the controller:
[self dismissViewControllerAnimated:NO completion:nil];
Again, everything is working.
But the view that brings up the imagePicker Controller no longer receives touch (the UIButton will no longer brings up the camera again) after the Modal View is dismissed.
It will only start to receive touches again when I switch to another view and come back.
I have been searching for a solution to this problem but have not been successful in finding anything. Thanks in advance.
EDIT (Adding code):
In CameraController.m
This is where brings up the Camera Roll
(Subclass of UIViewController conforming to
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>)
//UIButton that brings up the imagePicker
- (IBAction)useCamera
{
[self prepareImagePicker];
[self presentViewController:self.imagePicker animated:NO completion:nil];
}
//Initializing ImagePicker
- (void)prepareImagePicker
{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage, nil];
self.imagePicker.delegate = self;
OverlayCameraView *cameraOverlay = [[OverlayCameraView alloc] initWithFrame:(CGRect){{0, 0}, 320, 480} andTheCameraController:self];
self.imagePicker.cameraOverlayView = cameraOverlay;
}
}
//Delegate Method when a picture is taken
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
self.imagePicker.delegate = nil;
[picker dismissViewControllerAnimated:NO completion:nil];
self.imagePicker = nil;
self.imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
In OverlayCameraView.m
//Camera Overlay Class (Subclass of UIView)
- (id)initWithFrame:(CGRect)frame andTheCameraController:(CameraController*)cameraController
{
if ((self = [super initWithFrame:frame]))
{
self.camera = cameraController;
//.…2 UIButtons set up
}
return self;
}
//Overlay Cancel Button
- (void) cancel
{
[self.camera dismissViewControllerAnimated:NO completion:nil];
}
//Overlay Take Picture Button
- (void) takeAPicture
{
[self.camera.imagePicker takePicture];
}
You should be calling dismissViewControllerAnimated on the picker not self via the UIImagePickerControllerDelegate methods.
[picker dismissViewControllerAnimated:NO completion:nil];
or if you are keeping a reference to it:
[self.imagePicker dismissViewControllerAnimated:NO completion:nil];
EDIT:
Based on the revised code, I believe
self.imagePicker.cameraOverlayView = cameraOverlay;
should be:
self.imagePicker.cameraOverlayView = cameraOverlay.view;
Also this will cause a +2 reference count and will leak if you are not using ARC:
self.imagePicker = [[UIImagePickerController alloc] init];
It should be for proper reference counting:
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
self.imagePicker = picker;
[picker release];