handling captureStillImageAsynchronouslyFromConnection:completionHandler: - ios

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

Related

UIImagePickerController works in iOS 11.1 but not in iOS 11.4

I have a fairly typical set of controls to take a picture or choose from a user's photo library. The latest OS version I have in Xcode is 11.1, and the image picker works with the code I have. (I don't know whether it's possible to run a newer version on the simulator yet.)
When I run the code on an actual iPhone (5s with iOS 11.4), I get a discovery error from the image picker:
Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}
Trying to use the camera simply results in returning to the view controller, apparently with no action taken on the new image data, and no error messages.
EDIT: I have camera and photo library permissions to the info.plist, but they don't seem to affect this issue.
Here's the relevant code (the VC does several other unrelated things):
UserProfileViewController.h
#import <UIKit/UIKit.h>
#interface UserProfileViewController : UIViewController <NSURLSessionDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIScrollViewDelegate>
{
__weak IBOutlet UIScrollView *scrolview;
}
#end
UserProfileViewController.m:
#import "UserProfileViewController.h"
. . .
- (IBAction)takePhoto:(id)sender {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertController *errAlertController = [UIAlertController alertControllerWithTitle:#"Whoa!" message:#"This phone doesn't have a camera." preferredStyle:UIAlertControllerStyleAlert];
[errAlertController addAction:[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:errAlertController animated:YES completion:nil];
}
else
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
}
- (IBAction)ChooseFromGallery:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:^{
UIImage *chosenImage = [info objectForKey:UIImagePickerControllerEditedImage];
if ((chosenImage.size.height > 600.0) || (chosenImage.size.width > 800.0)){ // Need to scale down?
UIGraphicsBeginImageContextWithOptions(CGSizeMake(800.0f, 600.0f), NO, 0.0);
[chosenImage drawInRect:CGRectMake(0, 0, 800, 600)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self uploadPhoto:scaledImage];
}
else {
[self uploadPhoto:chosenImage];
}
// "uploadPhoto" takes the JPG representation of the image and uploads it to a specific server path using HTTP POST. As mentioned, it worked in the simulator for iOS 11.1.
}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
It turns out that the image actually was being updated in the uploadPhoto function, but it wasn't displaying until after the app had been destroyed and relaunched.
Apparently this effect resulted from the image processing being done in the completion block of dismissViewController, and not within the main block of didFinishPickingMediaWithInfo. Setting the completion block to NULL and moving the scaling and upload code fixed that problem.

iOS App crashes when user denies permission to Photos after Move & Scale in UIImagePickerController

I am using the default UIImagePickerController with allowsEditing set to YES for taking photo. When the user moves and scales the photo, the OS asks for access to 'Photos'.
The app crashes if the user denies access.
- (void)openCamera
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.editing = YES;
imagePickerController.allowsEditing = YES;
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];
}
And UIImagePickerControllerDelegate method goes like
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = info[UIImagePickerControllerEditedImage];
if (!image) {
image = info[UIImagePickerControllerOriginalImage];
}
self.profileImage = image;
[picker dismissViewControllerAnimated:YES completion:nil];
}
Crash message:
*** This application is not allowed to access Photo data.
I'm wondering why it should ask for access to Photos in the first place.
Any thoughts?
Use Assets Framework to test if your app is allowed to access photo data.
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
if(status==ALAuthorizationStatusAuthorized) {
..your code...
}

Programmatically Take Picture Without User Interaction

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.

ImagePicker loads very long for the second time

I have a problem with iOS ImagePicker. It works correctly, but when I want to take another photo the camera loads for a very long time (first time: 1-2sec, second time and later: 8-10sec). This is how I use it:
- (void)takePhoto {
_imagePicker = [UIImagePickerController new];
_imagePicker.delegate = self;
_imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
_imagePicker.allowsEditing = NO;
[self presentViewController:_imagePicker animated:YES completion:nil];
}
and this is how I get the image:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
[_imagePickerView.imageView setImage:image];
}
I don't see there much place for bugs. Of course I implement protocols UIImagePickerControllerDelegate and UINavigationControllerDelegate.
Do you have any ideas how can I figure out what is causing this?
You may want to dismiss the UIImagePickerController after you picked an item by calling
[_imagePicker dismissViewControllerAnimated:YES completion:nil];
It should be fine, now.
Try changing your first block of code to reuse the same imagePicker you have already initialized:
- (void)takePhoto {
if(_imagePicker == nil)
_imagePicker = [UIImagePickerController new];
_imagePicker.delegate = self;
_imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
_imagePicker.allowsEditing = NO;
[self presentViewController:_imagePicker animated:YES completion:nil];
}

black UIImagePicker preview ios7

I'm getting a black image picker preview. This DOES NOT happen if I delete the app then run it again. So the first time the app runs after being installed to my device it works. However if I bring up my image picker controller after that and capture an image, the preview is black. I have read all the answers mentioning background threading but I am not running any background threads. Im not sure what is causing this. It works perfectly the first time around but after that it never works.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//Image Picker settings
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = NO;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
[self presentViewController:self.imagePicker animated:NO completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
//dismiss view controller
[self dismissViewControllerAnimated:YES completion:nil];
}
}
Don't present the image picker from the viewDidLoad method of your view controller. When viewDidLoad is being run, your view controller hasn't yet been added to the view controller hierarchy. You can try presenting it from viewDidAppear, and that might fix it.

Resources