How do I get a photo from the phone gallery? - iphone

I have a huge problem as am trying to open image picker but don't know why my app crashes at this line
[self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];
I have used this this code in many application and code is working fine but in my current apps this code giving exception of preferred interface orientation.
The code which is used :
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (cameraAvailable == TRUE) {
if (buttonIndex == 0) {
imgpkrController.delegate = self;
imgpkrController.allowsEditing = YES;
imgpkrController.sourceType = UIImagePickerControllerSourceTypeCamera;
//[self presentModalViewController:(UIViewController *)imgpkrController animated:YES];
[self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];
}
else if(buttonIndex == 1){
imgpkrController.delegate = self;
imgpkrController.allowsEditing = YES;
imgpkrController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//[self presentModalViewController:(UIViewController *)imgpkrController animated:YES];
[self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];
}
}
if (buttonIndex == 0) {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
imgpkrController.delegate = self;
imgpkrController.allowsEditing = YES;
imgpkrController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//imgpkrController.modalPresentationStyle=UIModalPresentationFullScreen;
//[self presentModalViewController:imgpkrController animated:YES];
[self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];
//[self presentViewController:imgpkrController animated:YES completion:nil];
}
else{
NSLog(#"ipad photo");
imgpkrController.delegate = self;
popoverController = [[UIPopoverController alloc] initWithContentViewController:imgpkrController];
[popoverController presentPopoverFromRect:CGRectMake(260.0, 0.0, 400.0, 570.0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
else{
if (buttonIndex == 0) {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
imgpkrController.delegate = self;
imgpkrController.allowsEditing = YES;
imgpkrController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//[self presentModalViewController:(UIViewController *)imgpkrController animated:YES];
[self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];
}else{
NSLog(#"ipad photo");
imgpkrController.delegate = self;
popoverController = [[UIPopoverController alloc] initWithContentViewController:imgpkrController];
[popoverController presentPopoverFromRect:CGRectMake(260.0, 0.0, 400.0, 570.0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
}
}
Code crashes on this line
[self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];
with exception:
2013-07-16 15:11:02.143 HMW[1335:c07] *** Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'preferredInterfaceOrientationForPresentation must return a supported interface orientation!'
*** First throw call stack:
(0x2ec5012 0x2a7ce7e 0x2ec4deb 0x17a54da 0x19a444c 0x17a24f3 0x17a2777 0x2a663 0x1a952c1 0x2a90705 0x16bb920 0x16bb8b8 0x177c671 0x177cbcf 0x177bd38 0x16eb33f 0x16eb552 0x16c93aa 0x16bacf8 0x38a7df9 0x38a7ad0 0x2e3abf5 0x2e3a962 0x2e6bbb6 0x2e6af44 0x2e6ae1b 0x38a67e3 0x38a6668 0x16b865c 0x2802 0x2735)
libc++abi.dylib: terminate called throwing an exception

Not sure what's causing your error message as it seems to be related to the device orientation but I managed to get this working with the following code:
- (IBAction)attachPhotosClicked:(id)sender
{
if (![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear])
{
_imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[_popoverController presentPopoverFromRect:self.attachButton.frame inView:self.view
permittedArrowDirections:(UIPopoverArrowDirectionLeft)
animated:YES];
return;
}
else
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Choose a source"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Camera", #"Photo Library", nil];
[actionSheet showFromRect:self.attachButton.frame inView:self.view animated:YES];
}
}

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

You will find this issue in iOS 6.0. have a look at the following link,
UIPopoverController orientation crash in iOS 6
It should be fixed in later versions.

I think your problem is due to an iOs version problem. The interfaceOrientation protocol is changed in iOs 6.
I think this link could help you.
preferredInterfaceOrientationForPresentation must return a supported interface orientation?

Related

Xcode 5.1.1 expected identifier or '('

it says when i try and run my app that I need a expected identifier or '(' for this line of code:
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
it is in this line of code:
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
[self alert:#"PhotoLibrary is not supportted in this device."];
return;
}
imagePicker = imagePickerPhoto;
[self presentModalViewController:imagePicker animated:YES];
else{
if(!popover){
imagePicker = imagePickerPhoto;
popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[popover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}else{
[popover dismissPopoverAnimated:YES];
[popover release]; popover = nil;
}
Please copy and paste from XCode, and enclose your code in 'code brackets'
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
[self alert:#"PhotoLibrary is not supportted in this device."];
return;
}
imagePicker = imagePickerPhoto;
[self presentModalViewController:imagePicker animated:YES];
else
{
if(!popover) {
imagePicker = imagePickerPhoto;
popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[popover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else {
[popover dismissPopoverAnimated:YES];
[popover release];
popover = nil;
}
Looks like your first 'else' is in the wrong place, and you're missing a closing }

Viewcontroller not appearing since iOS7

Edit - I solved this myself - see the notes at the bottom
When using iOS7 on Xcode 5, I am using an option to take an image from a camera, or from the photo library, once the image is chosen (or a new picture taken) the view should flip over to the next screen.
This does not happen on the iPhone running iOS7, it works fine on the iPad, but the method is slightly different, but it does appear to be iPhone only problem on iOS7.
here is the code used, for example, on the choose image from library function;
-(void) choosePic {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
cameraUI.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeSavedPhotosAlbum];
cameraUI.allowsEditing = NO;
cameraUI.delegate = self;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
_popover = [[UIPopoverController alloc] initWithContentViewController:cameraUI];
[_popover presentPopoverFromRect:btnLibrary.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else
[self presentModalViewController: cameraUI animated: YES];
}
}
Also, the code once picker is finished;
- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info {
//Disable buttons
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self disableButtons];
//Get image
self.originalImage = (UIImage *) [info objectForKey: UIImagePickerControllerOriginalImage];
//Dismiss
if(_popover)
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
[_popover dismissPopoverAnimated:YES];
_popover = nil;
}
}
else
[picker dismissModalViewControllerAnimated: YES];
//Next
[self performSelector: #selector(nextScreen) withObject:nil afterDelay:0.5];
}
I fixed this by switching out;
[picker dismissModalViewControllerAnimated: YES];
With
[picker dismissViewControllerAnimated:NO completion:nil];
First you need to make sure that logic reaches the correct place it's intended to, try setting a breakpoint or NSLog before the iPhone's specific line, try this (You also missed curled braces, added them here) :
-(void) choosePic {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
cameraUI.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeSavedPhotosAlbum];
cameraUI.allowsEditing = NO;
cameraUI.delegate = self;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
_popover = [[UIPopoverController alloc] initWithContentViewController:cameraUI];
[_popover presentPopoverFromRect:btnLibrary.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}else{
NSLog(#"Checkpoint !");
[self presentModalViewController: cameraUI animated: YES];
}
} }

How to to make this image picker work on iPad?

I need help getting this code to work on iPad, it works fine on iPhone but for what ever reason not iPad.I have no idea what to do get this image picker to work on iPad. Any help will be appreciated.
-(IBAction)getCameraPicture:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsImageEditing = YES;
picker.sourceType = (sender == takePictureButton) ? UIImagePickerControllerSourceTypeCamera :
UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController: picker animated:YES];
[picker release];
}
-(IBAction)selectExitingPicture
{
if([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker= [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage : (UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
imageView.image = image;
[picker dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
[picker dismissModalViewControllerAnimated:YES];
}
On iPad you need to show it inside a UIPopoverController, not present it modally.
You can display the UIImagePicker as a modalView in iPhone. But in iPad you need to use UIPopover as a container for displaying the imagePicker.
Re-write your code like:
-(IBAction)selectExitingPicture
{
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker= [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
self.popover = [[UIPopoverController alloc]
initWithContentViewController:picker];
popover.delegate = self;
[self.popover presentPopoverFromRect:CGRectMake(0,0,170,250)
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
else
{
[self presentModalViewController:picker animated:YES];
}
[picker release];
}
In your #interface add the necessary protocols and necessary instances
#interface yourController: UIViewController
<UIImagePickerControllerDelegate,
UINavigationControllerDelegate, UIPopoverControllerDelegate>
{
UIPopoverController *popover;
}
#property (nonatomic, strong) UIPopoverController *popover;
#end
It'll work on both iPad and iPhone.
You can try this:
Change the CGRectMake for custom popover and CGSizeMake for content size.
- (IBAction)imageFromAlbum:(id)sender
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
// es iPad
if ([[UIDevice currentDevice]userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
//Averiguar si está en portrait o landscape
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
//PORTRAIT
if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
[self cerrarTeclado];
self.popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
self.popover.delegate = self;
[self.popover presentPopoverFromRect:CGRectMake(600, 400, 311, 350) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
[self.popover setPopoverContentSize:CGSizeMake(330, 515)];
}
//LANDSCAPE
if (orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft)
{
self.popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
self.popover.delegate = self;
[self.popover presentPopoverFromRect:CGRectMake(850, 400, 311, 350) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
[self.popover setPopoverContentSize:CGSizeMake(330, 515)];
}
} else {
// no es iPad
[self presentViewController:imagePicker animated:YES completion:nil];
}
}

Universal Xcode Camera Roll App Issue

I have a universal app in Xcode. If the user is using an iPad the use image from library button works great. However if they use an iPhone the button doesn't work.
Here is the error I receive.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPopoverController initWithContentViewController:] called when not running under UIUserInterfaceIdiomPad.'
Was told this code would work. Does it go into the (IBAction) Code below?
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { Add Popover code here } else {
Add alternative for popover here }
- (IBAction) useCameraRoll: (id)sender
{
if ([self.popoverController isPopoverVisible]) {
[self.popoverController dismissPopoverAnimated:YES];
} else {
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = YES;
newMedia = NO;
}
}
}
This crash is occuring because you can't use UIPopOverControllers on iPhones, only on iPads.
[UIPopoverController initWithContentViewController:] called when not
running under UIUserInterfaceIdiom Pad.'
Try something like this:
- (IBAction)useCameraRoll:(id)sender
{
UIPopoverController *popoverController = [[UIPopoverController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = YES;
newMedia = NO;
}
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
if (popoverController.isPopoverVisible) {
[popoverController dismissPopoverAnimated:YES];
}else{
[popoverController setContentViewController:imagePicker];
[popoverController setDelegate:self];
[popoverController presentPopoverFromRect:CGRectMake(0, 0, 320, 320) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}else{
[imagePicker presentViewController:imagePicker animated:YES completion:nil];
}
}

Selecting image on iPad

I'm trying to write code to select an image from the photo library on an iPad. The code that I'm using is below (taken from Apple's website), but it keeps giving me an error saying that On iPad, UIImagePickerController must be presented via UIPopoverController. I've tried changing this line: UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init]; to use a UIPopoverController, but obviously I'm doing something wrong because its not working.
- (BOOL) selectImage: (UIViewController*) controller
usingDelegate: (id <UIImagePickerControllerDelegate,
UINavigationControllerDelegate>) delegate {
if (([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum] == NO)
|| (delegate == nil)
|| (controller == nil))
return NO;
UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
// Displays saved pictures and movies, if both are available, from the
// Camera Roll album.
mediaUI.mediaTypes =
[UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypeSavedPhotosAlbum];
// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
mediaUI.allowsEditing = NO;
mediaUI.delegate = delegate;
[controller presentModalViewController: mediaUI animated: YES];
return YES; }
The Apple Developer page also says: "On iPad, you can alternatively present the browser interface using a popover as described in initWithContentViewController: and “Presenting and Dismissing the Popover” in UIPopoverController Class Reference." I've read it but I still can't get it to work. Any help would be appreciated.
Something like this should work:
// create an image picker controller
UIImagePickerController *imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
imagePickerController.delegate = self;
// present it in a popover
self.popoverController = [[[UIPopoverController alloc] initWithContentViewController:imagePickerController] autorelease];
self.popoverController.delegate = self;
// I was presenting the popover from a button, but you would set the rect to whatever is appropriate for your app
[self.popoverController presentPopoverFromRect:((UIButton *)sender).bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Declare an ivar such as:
UIPopoverController *imagePopover;
Then:
imagePopover = [[UIPopoverController alloc] initWithContentViewController:mediaUI];
[imagePopover setDelegate:self];
[imagePopover presentPopoverFromRect:someRect
inView:someView
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
in place of
[controller presentModal...];
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[popover presentPopoverFromRect:CGRectMake(0.0, 0.0, 400.0, 400.0)
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
refer below link
http://www.techotopia.com/index.php/An_Example_iOS_4_iPad_Camera_and_UIImagePickerController_Application_%28Xcode_4%29
I went through a lot of pain coming up with a solution which works on both iPad and iPhone, this is the final code which some of it comes from comments of other people:
the code has some bugs but it's a very good place to start :)
it covers permissions needed as well as how to manage viewcontrollers popoing up on ipad.
requires following imports :
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
and add this delegates :
UIActionSheetDelegate, UIImagePickerControllerDelegate
definitions :
__weak IBOutlet UIButton *attachButton;
UIImage *image;
button's action :
- (IBAction)doAttach:(id)sender {
UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:#"Select image from" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"From library",#"From camera", nil] ;
[action showInView:self.view];
}
#pragma mark - ActionSheet delegates
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if( buttonIndex == 1 ) {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if(authStatus == AVAuthorizationStatusAuthorized)
{
NSLog(#"%#", #"You have camera access");
}
else if(authStatus == AVAuthorizationStatusDenied)
{
NSLog(#"%#", #"Denied camera access");
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if(granted){
NSLog(#"Granted access to %#", AVMediaTypeVideo);
} else {
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#“no camera access“
message: #“if you need to use camera in this application go to settings -> appName -> and turn on camera.”
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#“ok” style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
NSLog(#"Not granted access to %#", AVMediaTypeVideo);
return ;
}
}];
}
else if(authStatus == AVAuthorizationStatusRestricted)
{
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#“no camera access“
message: #“if you need to use camera in this application go to settings -> appName -> and turn on camera.”
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#“ok” style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
NSLog(#"%#", #"Restricted, normally won't happen");
}
else if(authStatus == AVAuthorizationStatusNotDetermined)
{
NSLog(#"%#", #"Camera access not determined. Ask for permission.");
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if(granted){
NSLog(#"Granted access to %#", AVMediaTypeVideo);
} else {
NSLog(#"Not granted access to %#", AVMediaTypeVideo);
return ;
}
}];
}
else
{
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#“No camera access“
message: #“error accusing camera”
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#“ok” style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
return;
//NSLog(#"%#", #"Camera access unknown error.");
}
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *pickerView =[[UIImagePickerController alloc]init];
pickerView.allowsEditing = YES;
pickerView.delegate = self;
pickerView.sourceType = UIImagePickerControllerSourceTypeCamera;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
[ self.presentedViewController dismissViewControllerAnimated:YES completion:nil ];
pickerView.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController *popPC = pickerView.popoverPresentationController;
popPC.sourceView = attachButton;
popPC.permittedArrowDirections = UIPopoverArrowDirectionAny;
[self presentViewController:pickerView animated:YES completion:nil];
} else {
[self presentModalViewController:pickerView animated:YES ];
}
}
}else if( buttonIndex == 0 ) {
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
switch (status) {
case ALAuthorizationStatusRestricted:
case ALAuthorizationStatusDenied:
{
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#“no access to library”
message: #“if you wish to access photos in this app go to settings -> appName-> and turn on photos .”
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#“ok” style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
break;
default:
{
UIImagePickerController *pickerView = [[UIImagePickerController alloc] init];
pickerView.allowsEditing = YES;
pickerView.delegate = self;
[pickerView setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
[ self.presentedViewController dismissViewControllerAnimated:YES completion:nil ];
pickerView.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController *popup = pickerView.popoverPresentationController;
popup.sourceView = attachButton;
popup.permittedArrowDirections = UIPopoverArrowDirectionAny;
[self presentViewController:pickerView animated:YES completion:nil];
} else {
[self presentModalViewController:pickerView animated:YES ];
}
}
break;
}
}
}
#pragma mark - PickerDelegates
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[self dismissModalViewControllerAnimated:true];
UIImage * img = [info valueForKey:UIImagePickerControllerEditedImage];
image = img;
}