Open UIImagePickerController when view loads

2019-06-05 06:26发布

问题:

I have a UIViewController, that I would like upon loading the view - to open a UIImagePickerController for the camera.

A. I wrote the following: EDITED - I am using now viewDidAppear

- (void)viewDidAppear:(BOOL)animated{
    imagePicker = [[UIImagePickerController alloc] init];
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
    }
    else{
        [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    }
    [imagePicker setDelegate:self];
    [self presentModalViewController:imagePicker animated:YES];
}

the problem is, that after the user takes a snap shot or selects an image from folder - it keeps on loading the UIImagePicker again - how can I make it call it only once?

B. I would like the camera to be displayed inside a rectangle (could be UIImage or UIView) and not full screen so I will be able to leave my top navigation bar.

How can I achieve the above?

回答1:

For case A, better put this in viewDidAppear instead of viewWillAppear to avoid animation collisions (and wait_fences error)

For case B, you either need a capture session, or need to tweek the UIImagePickerController using a transform. You can do that thanks to the follow UIImagePickerController properties:

  • cameraViewTransform to change the CGAffineTransform applied to the view that displays the camera/video. Thus you will be able to scale it down for example
  • showsCameraControls that you can set to NO to hide the default iOS controls if you plan to add your own UIButtons instead
  • cameraOverlayView that allows you to put an arbitrary UIView atop of the camera view. You typically use a view that has a backgroundColor = [UIColor clearColor] and add some subviews to it, especially some decorative UIImageView if needed (a frame for example), some UIButtons to trigger the camera (and use the takePicture, startVideoCapture and stopVideoCapture methods of UIImagePickerController in their corresponding IBAction as needed) and replace the build-it camera controls you hid with the previous property, and so on

In your case, if you want to keep the regular camera's button but just want to change the frame and make it not-fullscreen, the cameraTransform property is then typically what you need. Adding a decorative frame around will simply consist of using an UIImageView for the cameraOverlayView property and use it to display the image of the decorative frame (image with its center transparent so that the camera view is visible thru it, of course)


[EDIT] Note that (according to the documentation) the cameraTransform property is only available when capturing movies (when sourceType == UIImagePickerControllerSourceTypeCamera). So you can use it to let the user take a picture from its camera, but if you want to use your UIImagePickerController to choose a picture from the Library, you can't use the cameraTransform (and that quite logical, as it would seem strange to display onscreen). So be sure to use it only in that case, and especially avoid it when the UIImagePickerControllerSourceTypeCamera source type is not available on the device.



回答2:

Just implement your action into the viewDidLoad method. This is what I did and it works perfectly fine now.



回答3:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self getMediaFromSource:UIImagePickerControllerSourceTypeCamera];

}
- (IBAction)TakePhoto:(id)sender {
    [self getMediaFromSource:UIImagePickerControllerSourceTypeCamera];
}

- (void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
    picker.delegate         = self; 
    picker.allowsEditing        = NO; 
    picker.sourceType           = sourceType; 
    [self presentModalViewController:picker animated:YES];

    [picker release];
}

#pragma mark - UIImagePickerController delegate methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [picker dismissModalViewControllerAnimated:YES];
    [self.tabBarController setSelectedIndex:1];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 

    [picker dismissModalViewControllerAnimated:YES];
    [self.tabBarController setSelectedIndex:1];
}

Updated Part B. Example code of cameraOverlayView

- (void)setupImagePicker:(UIImagePickerControllerSourceType)sourceType
 {
    self.imagePickerController.sourceType = sourceType;

if (sourceType == UIImagePickerControllerSourceTypeCamera)
{
    // user wants to use the camera interface
    //
    self.imagePickerController.showsCameraControls = NO;

    if ([[self.imagePickerController.cameraOverlayView subviews] count] == 0)
    {
        // setup our custom overlay view for the camera
        //
        // ensure that our custom view's frame fits within the parent frame
        CGRect overlayViewFrame = self.imagePickerController.cameraOverlayView.frame;
        CGRect newFrame = CGRectMake(0.0,
                                     CGRectGetHeight(overlayViewFrame) -
                                     self.view.frame.size.height - 10.0,
                                     CGRectGetWidth(overlayViewFrame),
                                     self.view.frame.size.height + 10.0);
        self.view.frame = newFrame;
        [self.imagePickerController.cameraOverlayView addSubview:self.view];
    }
 }
}

http://developer.apple.com/library/ios/#samplecode/PhotoPicker/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010196

I would suggest playing with the "cameraOverlayView," to try and create some sort of pseudo navigation bar in which the interaction will work as you intended.

"You can customize an image picker controller to manage user interactions yourself. To do this, provide an overlay view containing the controls you want to display, and use the methods described in “Capturing Still Images or Movies.” You can display your custom overlay view in addition to, or instead of, the default controls. Custom overlay views for the UIImagePickerController class are available in iOS 3.1 and later by way of the cameraOverlayView property. For a code example, see the PhotoPicker sample code project.

Important The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified, with one exception. You can assign a custom view to the cameraOverlayView property and use that view to present additional information or manage the interactions between the camera interface and your code."

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html