I'm having an issue presenting the UIImagePickerController on iOS 7 devices. I use the following code to present the image picker.
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
cameraUI.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
cameraUI.delegate = self;
[[self presentViewController:cameraUI animated:YES completion:NULL];
After the call to presentViewController, the application crashes due to an exec bad access. The console reports the following exceptions.
[SBSAccelerometer valueRestriction]: unrecognized selector sent to instance 0x1650e360
[__NSCFNumber valueRestriction]: unrecognized selector sent to instance 0x146d0e70
I enabled zombies to see if an object is getting deallocated prematurely. Zombies reports the following exceptions:
[NSISRestrictedToNonNegativeVariable retain]: message sent to deallocated instance 0x156f0010
Any thoughts?
EDIT
Here is the stack trace I receive with zombies enabled:
This is a bug in iOS 7 on iPad. It appears the solution for now is to request permission to photos before opening the UIPopoverControl. Here is how I implemented my solution:
**// Photo Library
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
void(^blk)() = ^() {
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if (NIIsPad()) {
UIPopoverController* popover = [[UIPopoverController alloc] initWithContentViewController:picker];
[popover presentPopoverFromBarButtonItem:self.popoverAnchor permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
[self.navigationController presentModalViewController:picker animated:YES];
}
};
// Make sure we have permission, otherwise request it first
ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init];
ALAuthorizationStatus authStatus;
if (IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(#"6.0"))
authStatus = [ALAssetsLibrary authorizationStatus];
else
authStatus = ALAuthorizationStatusAuthorized;
if (authStatus == ALAuthorizationStatusAuthorized) {
blk();
} else if (authStatus == ALAuthorizationStatusDenied || authStatus == ALAuthorizationStatusRestricted) {
[[UIAlertView alertViewWithTitle:#"Grant photos permission" message:#"Grant permission to your photos. Go to Settings App > Privacy > Photos."] show];
} else if (authStatus == ALAuthorizationStatusNotDetermined) {
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
// Catch the final iteration, ignore the rest
if (group == nil)
dispatch_async(dispatch_get_main_queue(), ^{
blk();
});
*stop = YES;
} failureBlock:^(NSError *error) {
// failure :(
dispatch_async(dispatch_get_main_queue(), ^{
[[UIAlertView alertViewWithTitle:#"Grant photos permission" message:#"Grant permission to your photos. Go to Settings App > Privacy > Photos."] show];
});
}];
}
}**
Don't forget to add AssetsLibrary.framework to your project.
Related
I am checking camera and photo permission on my App when user select or capture any image.
I am using this code.
-(void)choosePhotoFromExistingImages
{
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
if (status == ALAuthorizationStatusDenied || status == ALAuthorizationStatusRestricted) {
[APPDELEGATE showAlertViewForPhotos];
//show alert for asking the user to give permission
}
else{
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
controller.allowsEditing = YES;
controller.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil] ;
controller.delegate = self;
[self presentViewController: controller animated: YES completion: nil];
}
}
}
The code is working fine but when user first time choose Don't allow photo library it display a black screen like this same thing happening in camera.
What i need is that when user press on don't allow i can check that user has cancel the permission so i can dismiss imagepicker or camera .
You can use ALAssetsLibrary authorizationStatus to check it
How do I react to a user choosing "Don't Allow" when asking for permission to access Photos?
In my app,I have used the following code to access the camera to change the profile pic of the user.But I can't access the camera.
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
if(isIOS8SystemVersion)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self presentViewController:picker animated:YES completion:NULL];
}];
}
else
{
[self presentViewController:picker animated:YES completion:NULL];
}
This code works fine on iPhone 4(16gb) with iOS 7.x .But in iOS 9,the presented viewController appears as a black screen and I can't take the picture.The same is the case with the photoLibrary.The same code works well for my other app in the same device.
I went through many questions in Stack Overflow and tried many solutions.But they did not work for me.Like making a singleton for UIImagePIcker controller etc..etc.
In settings->privacy->camera-> I can't see my app listed in this section.
What all are the things to be done in order that an iOS app can access Camera and photos?Am I missing anything?
Just delay your function to open camera and it will work fine :D
Use below code:
[self performSelector:#selector(showCamera) withObject:nil afterDelay:1.0];
-(void)showCamera
{
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
if(isIOS8SystemVersion)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self presentViewController:picker animated:YES completion:NULL];
}];
}
else
{
[self presentViewController:picker animated:YES completion:NULL];
}
}
first call this method and determine permission of camera
-(void)PermissionForCamera
{
NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
if(authStatus == AVAuthorizationStatusAuthorized)
{
NSLog(#"AVAuthorizationStatusAuthorized");
// run your code for camera
ImagePicker = [[UIImagePickerController alloc] init];
[ImagePicker setDelegate:self];
ImagePicker = UIImagePickerControllerSourceTypeCamera;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self ImagePicker animated:YES completion:nil];
}];
}
else if(authStatus == AVAuthorizationStatusNotDetermined)
{
NSLog(#"AVAuthorizationStatusNotDetermined");
// run your code for camera
ImagePicker = [[UIImagePickerController alloc] init];
[ImagePicker setDelegate:self];
ImagePicker = UIImagePickerControllerSourceTypeCamera;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self ImagePicker animated:YES completion:nil];
}];
}
else
{
// turn on the camera permission from settings
}
}
I want to access camera in my app. I am trying the following code.
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
if(isIOS8SystemVersion)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self presentViewController:picker animated:YES completion:NULL];
}];
}
else
{
[self presentViewController:picker animated:YES completion:NULL];
}
}
This code works on my other app perfectly.But in this app,it is not asking camera permissions or showing it in the settings->privacy->camera.
The app prompts to use the location.But not showing anything for the camera or photos.
The black screen appears and I can't take the picture if I directly use the camera code without the condition check.
I had the exactly same issue for couple of days,
Try this its solved my problem, make sure that there is a value
(Application name as string) in your info.plist > "Bundle display name".
In my case it was empty and because of that it didn't work.
let me know if it helped you.
Use following method to check device camera authorizationStatus. If not it will prompt for Access, if rejected if will show alert to navigate to App settings.
- (void)checkCameraPermission
{
// *** check for hardware availability ***
BOOL isCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
if(!isCamera)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:APPName message:#"Camera not detected" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
// *** Store camera authorization status ***
AVAuthorizationStatus _cameraAuthorizationStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (_cameraAuthorizationStatus)
{
case AVAuthorizationStatusAuthorized:
{
_cameraAuthorizationStatus = AVAuthorizationStatusAuthorized;
// *** Camera is accessible, perform any action with camera ***
}
break;
case AVAuthorizationStatusNotDetermined:
{
NSLog(#"%#", #"Camera access not determined. Ask for permission.");
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted)
{
if(granted)
{
NSLog(#"Granted access to %#", AVMediaTypeVideo);
// *** Camera access granted by user, perform any action with camera ***
}
else
{
NSLog(#"Not granted access to %#", AVMediaTypeVideo);
// *** Camera access rejected by user, perform respective action ***
}
}];
}
break;
case AVAuthorizationStatusRestricted:
case AVAuthorizationStatusDenied:
{
// Prompt for not authorized message & provide option to navigate to settings of app.
dispatch_async( dispatch_get_main_queue(), ^{
NSString *message = NSLocalizedString( #"My App doesn't have permission to use the camera, please change privacy settings", #"Alert message when the user has denied access to the camera" );
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:APPName message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString( #"OK", #"Alert OK button" ) style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:cancelAction];
// Provide quick access to Settings.
UIAlertAction *settingsAction = [UIAlertAction actionWithTitle:NSLocalizedString( #"Settings", #"Alert button to open Settings" ) style:UIAlertActionStyleDefault handler:^( UIAlertAction *action ) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}];
[alertController addAction:settingsAction];
[self presentViewController:alertController animated:YES completion:nil];
});
}
break;
default:
break;
}
}
The code works in my app :
UIImagePickerController *picker;
if([self checkForCameraAcess])
{
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Camera",nil) message:NSLocalizedString(#"Access to camera seems to be turned off. Please enable it from settings",nil) delegate:self cancelButtonTitle:NSLocalizedString(#"OK",nil) otherButtonTitles:NSLocalizedString(#"Settings",nil), nil];
alert.tag = 101;
[alert show];
}
-(BOOL)checkForCameraAcess
{
BOOL isAccess = YES;
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
{
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
//Here we check condition for AVAuthorizationStatusNotDetermined, because when user install iLeads app first time in device (Nerver before iLeads app install in Device), then setup an event and tap on the scan button, at that time authStatus is AVAuthorizationStatusNotDetermined so its show alert for camera acess first. Then after our custom alert shows if we tap on 'Dont allow' button of the camera acess.
if(authStatus == AVAuthorizationStatusAuthorized || authStatus == AVAuthorizationStatusNotDetermined)
{
isAccess = YES;
}
else
{
isAccess = NO;
}
}
return isAccess;
}
Don't forget to add UIImagePickerControllerDelegate in your .h
I hope it will work.
This code for capturing image one by one from camera,but after taking one image next time camera will open but with black screen(like it,s shutter close).all other ios version its working,but not working in ios 8.please tell me how can i solve it?
-(void)openCamera
{
if(![PickerHandler doesDeviceSupportMediaType:ITEM_TYPE_PHOTO])
{
[PickerHandler showNoDeviceSupportWarningForMediaType:ITEM_TYPE_PHOTO withDelegate:self];
}
else
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
[self presentViewController:picker animated:YES completion:nil];
}
}
Go to Settings > Privacy > Pictures ... and check if your app have permission.
In the code, use this to verify camera access.
- (BOOL)authorizedCameraAccess
{
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
return (status == AVAuthorizationStatusAuthorized);
}
this code is not work in simulator.
UIImagePickerController *videoScreen = [[UIImagePickerController alloc] init];
videoScreen.sourceType = UIImagePickerControllerSourceTypeCamera;
videoScreen.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
videoScreen.allowsEditing = NO;
videoScreen.delegate = self;
[self presentViewController:videoScreen animated: YES completion:NO];
Implement This method
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:NO completion:NO];
}
I am struggling with UIImagePickerController as when i open camera and take image it show black screen in IOS 7 as this is working fine in ios 6, i have tried some other links what says but its not working, please help me with this...
and i am getting this error too
<Error>: CGAffineTransformInvert: singular matrix.
and now i got something ..
Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.
when i just click on take image button
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
dispatch_async(dispatch_get_main_queue(), ^{
if (buttonIndex == 0) {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
NSString *mediaType = AVMediaTypeVideo; // Or AVMediaTypeAudio
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
// This status is normally not visible—the AVCaptureDevice class methods for discovering devices do not return devices the user is restricted from accessing.
if(authStatus == AVAuthorizationStatusRestricted){
NSLog(#"Restricted");
}
// The user has explicitly denied permission for media capture.
else if(authStatus == AVAuthorizationStatusDenied){
NSLog(#"Denied");
}
// The user has explicitly granted permission for media capture, or explicit user permission is not necessary for the media type in question.
else if(authStatus == AVAuthorizationStatusAuthorized){
NSLog(#"Authorized");
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
UIImagePickerController *imagePickerCamera =[[UIImagePickerController alloc] init];
imagePickerCamera.delegate = self;
imagePickerCamera.allowsEditing = YES;
imagePickerCamera.sourceType = UIImagePickerControllerSourceTypeCamera;
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:imagePickerCamera animated:YES completion:^{}];
});
} else {
NSString *errorString = [NSString stringWithFormat:#"This device does not support this feature."];
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:#"Alert" message:errorString delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
}
// Explicit user permission is required for media capture, but the user has not yet granted or denied such permission.
else if(authStatus == AVAuthorizationStatusNotDetermined){
[AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
if(granted){
NSLog(#"Granted access to %#", mediaType);
}
else {
NSLog(#"Not granted access to %#", mediaType);
}
}];
}
else {
NSLog(#"Unknown authorization status");
}
}
else {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
dispatch_async(dispatch_get_main_queue(), ^{
UIImagePickerController *imagePickerCamera =[[UIImagePickerController alloc] init];
imagePickerCamera.delegate = self;
imagePickerCamera.mediaTypes = #[(NSString *) kUTTypeImage];
imagePickerCamera.allowsEditing = YES;
imagePickerCamera.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePickerCamera animated:YES completion:nil];
});
} else {
NSString *errorString = [NSString stringWithFormat:#"This device does not support this feature."];
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:#"Alert" message:errorString delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
}
} else if (buttonIndex == 1) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *imagePickerAlbum =[[UIImagePickerController alloc] init];
imagePickerAlbum.delegate = self;
imagePickerAlbum.mediaTypes = #[(NSString *) kUTTypeImage];
imagePickerAlbum.allowsEditing = YES;
imagePickerAlbum.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePickerAlbum animated:YES completion:nil];
} else {
NSString *errorString = [NSString stringWithFormat:#"This device does not support this feature."];
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:#"Alert" message:errorString delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
}
});
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
_upLoadimage = info[UIImagePickerControllerEditedImage];
}
I had the same problem with camera. My screen to modify or editing camera image was appearing black. The log showed me same error (CGAffineTransformInvert xx ). I did a lot of checks to determine or know where is the error.
In my application I use the component SVProgessHUD to show alerts and messages. Commenting lines with this use corrects the problem. I have updated to the last version of the component (1.0) and the error CGAffineTransformInvert disappears.