Programmatically Take Picture Without User Interaction - ios

How do I take Picture without User Interaction or without presenting ImagePickerController
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
}
// image picker needs a delegate,
[imagePickerController setDelegate:self];
// Place image picker on the screen
[self presentModalViewController:imagePickerController animated:YES];
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
_myImageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
I know this way I can take picture but automatically How I do accomplish?

You can use the takePicture method on UIImagePickerController to take a picture programmaticly.
But I suggest to use AVFoundation to create you onw image capture.

Related

UIImagePickerController delegate not called when sourceType is Photo Library

I have a problem where the delegate of UIImagePickerController not being called when presented with sourceType = UIImagePickerControllerSourceTypePhotoLibrary.
Oddly enough, it works perfectly fine in camera mode.
Interface declaration:
#interface ChatVC : UIViewController <UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
And in implementation:
- (void)takePhotoAction {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController* imgPickerController = [[UIImagePickerController alloc] init];
imgPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imgPickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
imgPickerController.allowsEditing = YES;
imgPickerController.delegate = self;
[self presentViewController:imgPickerController animated:YES completion:nil];
}
}
- (void)choosePhotoFromLibraryAction {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController* imgPickerController = [[UIImagePickerController alloc] init];
imgPickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imgPickerController.allowsEditing = YES;
imgPickerController.delegate = self;
[self presentViewController:imgPickerController animated:YES completion:nil];
}
}
And here's the delegate method implementation in the same view controller:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo {
NSLog(#"Image Picker Controller Picked Image - Deprecated");
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"Image Picker Controller Picked Image");
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
NSLog(#"Image Picker Controller Cancelled");
[picker dismissViewControllerAnimated:YES completion:nil];
}
When opened in camera mode, everything works just fine, and I can see the appropriate delegate methods being called.
But when opened in photo library mode and I choose a photo, nothing happens and no delegate method is called. However, I can see -imagePickerControllerDidCancel: delegate method being called when I tap on Cancel button.
I even suspected memory warning somehow invalidates the delegate, but it was not the case here.
Please someone help me figure this out.
Thanks in advance!

handling captureStillImageAsynchronouslyFromConnection:completionHandler:

Ive been trying to set up a small iOS method that takes a picture automatically when user opens the app. After much research i finally found this iOS taking photo programmatically and after a little more i found this from apple https://developer.apple.com/library/ios/samplecode/AVCam/Introduction/Intro.html can someone help me get started in setting up a method for captureStillImageAsynchronouslyFromConnection:completionHandler:
i don't want any interaction from said user. thanks
Even though you seem to want to do the call asynchronously and using AVFoundation, I still recommend simply using a UIImagePickerController in this case, ex:
- (void)viewDidLoad {
[super viewDidLoad];
// If the device has a camera...
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
// Create an image picker
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
imagePickerController.showsCameraControls = NO;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:^{
// And take the picture after a short delay
// to give the view and image picker time to get
// ready
[self performSelector:#selector(takepic:) withObject:imagePickerController afterDelay:2];
}];
}
}
// Automatically take the picture using the
// image picker passed in as a parameter
- (void)takepic:(UIImagePickerController*)imagePickerController {
[imagePickerController takePicture];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// ... Do whatever with the image ...
[picker dismissViewControllerAnimated:YES completion:nil];
}

UIImageViewController has no status after taking photo in preview

I use following code to take picture
self.imagePickerController = [[UIImagePickerController alloc] init];
self.imagePickerController.delegate = self;
self.imagePickerController.sourceType = sourceType;
self.imagePickerController.allowsEditing = YES;
self.imagePickerController.showsCameraControls = YES;
self.imagePickerController.wantsFullScreenLayout = YES;
[self presentViewController:self.imagePickerController animated:NO completion:nil];
after taking, it will jump to a preview with two button: retake and use
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
the above class method can't be called until i click the button use, so is there any class method can I use when entering preview view? thanks

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.

Capturing image without user interaction

I want to capture a photo in iOS without any user interaction by using the front camera. Can any one show me how to do that?
I used the takePicture function in UIImagePickerController class but it didn't work because the delegate method (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info never get called:
-(void)takePicture:(id)sender
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
//Use camera if device has one otherwise use photo library
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
}
else
{
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
[imagePicker setDelegate:self];
//Show image picker
[imagePicker takePicture];
}
Can anyone show me the correct way to do this?
You need to add the following code
//Getting image from front camera
imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
[self imagePicker animated:YES completion:nil];

Resources