Cancel button not working in UIImagePickerView - ios

I am use UIImagePickerView in my code in which three buttons are there. One to take photo, second to choose photo and third to cancel. The code as below:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select the operation to proceed?"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Take Photo", #"Select Photo", nil];
[actionSheet showInView:self.view];
When I click on the Cancel button it does not work.
I am using UIImagePickerViewDelegate method as below.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
#try {
//set selected image in imageview by imagepickerview
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
imgProfilePic.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:nil];
}
#catch (NSException *exception) {
NSLog(#"%#",exception.description);
}
}
//- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
//{
// NSLog(#"####");
// [picker dismissViewControllerAnimated:YES completion:NULL];
//}
Please check my code for UIImagePickerView and provide me guidance for correct code.
#pragma mark - Actionssheet delegate method for Image
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
#try {
if(buttonIndex != 3)
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
if(buttonIndex == 0)
{
NSLog(#"Choose Photo from Camera");
//simulator has no camera so app will crash, below call just provide the alert that device has no camera.
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Device has no camera"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[myAlertView show];
}
else
{
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
}
if(buttonIndex == 1)
{
NSLog(#"Choose Photo from Gallary");
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
// if (buttonIndex == 3)
// {
// NSLog(#"###");
// }
//
// else
// {
// NSLog(#"Cancel the tab");
// [picker dismissViewControllerAnimated:YES completion:NULL];
//
// }
[self presentViewController:picker animated:YES completion:nil];
}
}
#catch (NSException *exception) {
NSLog(#"%#",exception.description);
}
}

I think you are new for the stackOverflow and iOS application Development. That UIActionsheet and UIImagePickerController both are different things. So if you are create a actiohsheet like:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select the operation to proceed?"delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil otherButtonTitles:#"Take Photo", #"Select Photo", nil];
[actionSheet showInView:self.view];
Now you can called its button method like following:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0)
{
// take photo selected
}
else if (buttonIndex==1)
{
// select photo selected
}
else
{
// cancel button selected
}
}
You can also use UIAlertController that is the replacement of UIAlert and UIActionSheet from iOS8 so you can use like following:
UIAlertController* AlertSheet = [UIAlertController alertControllerWithTitle:#"New ActionSheet" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#"Action" style:UIAlertActionStyleDefault //
handler:^(UIAlertAction * action) {
NSLog(#"Action");
}];
[AlertSheet addAction:defaultAction];
UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}];
[AlertSheet addAction:cancleAction];
[self presentViewController:AlertSheet animated:YES completion:nil];

You can try this code its work for me
UIAlertController* alert = [UIAlertController
alertControllerWithTitle:nil // Must be "nil", otherwise a blank title area will appear above our two buttons
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* button0 = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
// UIAlertController will automatically dismiss the view
}];
UIAlertAction* button1 = [UIAlertAction
actionWithTitle:#"Take photo"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// The user tapped on "Take a photo"
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL]; }];
UIAlertAction* button2 = [UIAlertAction
actionWithTitle:#"Choose Existing"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// The user tapped on "Choose existing"
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}];
[alert addAction:button0];
[alert addAction:button1];
[alert addAction:button2];
[self presentViewController:alert animated:YES completion:nil];

UIActionSheet is deprecated since iSO 8. You should use UIAlertController
UIAlertController* alert = [UIAlertController
alertControllerWithTitle:nil // Must be "nil", otherwise a blank title area will appear above our two buttons
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* button0 = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
// UIAlertController will automatically dismiss the view
}];
UIAlertAction* button1 = [UIAlertAction
actionWithTitle:#"Take photo"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// The user tapped on "Take a photo"
UIImagePickerController *imagePickerController= [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}];
UIAlertAction* button2 = [UIAlertAction
actionWithTitle:#"Select Photo"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// The user tapped on "Select Photo"
UIImagePickerController *imagePickerController= [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}];
[alert addAction:button0];
[alert addAction:button1];
[alert addAction:button2];
[self presentViewController:alert animated:YES completion:nil];

Related

Application terminating while clicking camera use image button objective c

My application terminates showing Message from debugger: Terminated due to signal 9 after doing rigorous searching didn't find any thing , i have also checked for memory leaks but does not find any..
Problem Statement - When i open camera from my app and after capturing image when i select use image my app terminates.
My Code
- (void)imagePickerController:(UIImagePickerController )picker didFinishPickingMediaWithInfo:(NSDictionary )info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
UIImageWriteToSavedPhotosAlbum(chosenImage, nil, nil, nil);
[picker dismissViewControllerAnimated:YES completion:NULL];
}
I used to select image from both camera and gallery using imagepicker with uialertactionsheet. This code is working for me, so you try this. And use these delegate's
- (IBAction)Camera_Click:(id)sender
{
UIAlertController *alertView = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
// Photos from Gallery by calling Method
UIAlertAction *choose = [UIAlertAction actionWithTitle:#"Select from photos" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[self performSelector:#selector(choosePhotoFromGallery)];
[alertView dismissViewControllerAnimated:YES completion:nil ];
}];
//Take New Photo From Camara
UIAlertAction *Capture = [UIAlertAction actionWithTitle:#"Capture from camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[self performSelector:#selector(takePhotoFromCamara)];
[alertView dismissViewControllerAnimated:YES completion:nil ];
}];
UIAlertAction *Cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
}];
//Adding the alertView Actions
[alertView addAction:choose];
[alertView addAction:Capture];
[alertView addAction:Cancel];
//Presents the AlertView
[self presentViewController:alertView animated:YES completion:nil];
}
- (void)takePhotoFromCamara
{
UIImagePickerController *Take_pic = [[UIImagePickerController alloc] init];
Take_pic.delegate = self;
Take_pic.allowsEditing = YES;
Take_pic.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:Take_pic animated:YES completion:NULL];
}
- (void)choosePhotoFromGallery
{
UIImagePickerController *Choose_picker = [[UIImagePickerController alloc] init];
Choose_picker.delegate = self;
Choose_picker.allowsEditing = YES;
Choose_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:Choose_picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *imgPicker = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
self.your_imgView.image = imgPicker;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}
**Finally you add this two in your app info.plist file, otherwise app will crash.
<key>NSPhotoLibraryUsageDescription</key>
<string><$YourAppname$>We access your photo library.</string>
<key>NSCameraUsageDescription</key>
<string><$YourAppname$>We access your camera.</string>**

How to display only videos from photolibrary in ios

I am working on an iOS app. I want to selext some videos from iPhone library. I have done code as below but it also loads the photos from the photo library. My need is to load only videos on the device (not photos ONLY VIDEOS). So can anyone please help me what changes I need to for this goal? My code is as below:
UIAlertAction *Gallery = [UIAlertAction actionWithTitle:#"Add Existing Video" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
[alertcontroller dismissViewControllerAnimated:YES completion:nil];
//VideoViewController *videoVC = [[VideoViewController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//videoVC.sourceType = picker.sourceType;
self.picker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie];
NSArray *sourceTypes = [CustomCamera availableMediaTypesForSourceType:self.picker.sourceType];
//videoVC.pickerController = picker;
if (![sourceTypes containsObject:(NSString *)kUTTypeMovie ])
{
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:nil message:#"No Video available" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
else
{
[self presentViewController:self.picker animated:YES completion:nil];
}
}];

UIAlertView first deprecated IOS 9

I have tried several ways to use UIAlertController,instead of UIAlertView. I tried several ways but I cannot make the alert action work.
Here is my code that works fine in IOS 8 and IOS 9 but is showing up with deprecated flags. I tried the elegant suggestion below but I can't make it function in this context. I need to submit my app and this is the last thing to address. Thank You for any further suggestions. I am a newbie.
#pragma mark - BUTTONS ================================
- (IBAction)showModesAction:(id)sender {
NSLog(#"iapMade: %d", iapMade3);
// IAP MADE ! ===========================================
if (!iapMade3) {
//start game here
gamePlaysCount++;
[[NSUserDefaults standardUserDefaults]setInteger:gamePlaysCount forKey:#"gamePlaysCount"];
NSLog(#"playsCount: %ld", (long)gamePlaysCount);
if (gamePlaysCount >= 4) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Basic"
message: THREE_PLAYS_LIMIT_MESSAGE
delegate:self
cancelButtonTitle:#"Yes, please"
otherButtonTitles:#"No, thanks", nil];
[alert show];
NSString *path = [[NSBundle mainBundle] pathForResource:#"cow" ofType:#"wav"];
_pop =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[_pop play];
[self dismissViewControllerAnimated:true completion:nil];
} else {
if (gamePlaysCount == 1) {
// Create & store the next 5 mins when player gets 3 more lives
nextDateToPlay = [[NSDate date] dateByAddingTimeInterval:60*60*0.1];
NSLog(#"CURRENT DATE: %#", [NSDate date]);
NSLog(#"NEXT DAY: %#", nextDateToPlay);
[[NSUserDefaults standardUserDefaults]setObject: nextDateToPlay forKey:#"nextDateToPlay"];
NSLog(#"nextDateToPlay: %#", nextDateToPlay);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Basic"
message: THREE_PLAYS_LIMIT_MESSAGE2
delegate:self
cancelButtonTitle:#"Got it!"
otherButtonTitles:#"Start", nil];
[alert show];
} else {
if (gamePlaysCount == 3) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Basic"
message: THREE_PLAYS_LIMIT_MESSAGE3
delegate:self
cancelButtonTitle:#"Yep, I Know!"
otherButtonTitles:#"Start", nil];
[alert show];
}
}
}
}
}
// IAP NOT MADE =============================
#pragma mark - ALERTVIEW DELEGATE ============================
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:#"Yes, please"]) {
UIStoryboard *storyboard = self.storyboard;
MenuViewController *svc = [storyboard instantiateViewControllerWithIdentifier:#"Store"];
svc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:svc animated:YES completion:nil];
}
}
From iOS8 Apple provide new UIAlertController class which you can use instead of UIAlertView which is now deprecated, it is also stated in deprecation message:
UIAlertView is deprecated. Use UIAlertController with a preferredStyle
of UIAlertControllerStyleAlert instead
So you should use something like this
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:#"Title"
message:#"Message"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:#"Yes, please"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle your yes please button action here
}];
UIAlertAction* noButton = [UIAlertAction
actionWithTitle:#"No, thanks"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle no, thanks button
}];
[alert addAction:yesButton];
[alert addAction:noButton];
[self presentViewController:alert animated:YES completion:nil];
//Calling
[self showMessage:#"There is no internet connection for this device"
withTitle:#"Error"];
//Method
-(void)showMessage:(NSString*)message withTitle:(NSString *)title
{
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//do something when click button
}];
[alert addAction:okAction];
UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
[vc presentViewController:alert animated:YES completion:nil];
}
If you want to use this alert in NSObject class you should use like:
-(void)showMessage:(NSString*)message withTitle:(NSString *)title{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:alertController animated:YES completion:^{
}];
});
}
Swift version of new implementation is :
let alert = UIAlertController(title: "Oops!", message:"your message", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Okay.", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}
Xcode 8 + Swift
Assuming self is a UIViewController:
func displayAlert() {
let alert = UIAlertController(title: "Test",
message: "I am a modal alert",
preferredStyle: .alert)
let defaultButton = UIAlertAction(title: "OK",
style: .default) {(_) in
// your defaultButton action goes here
}
alert.addAction(defaultButton)
present(alert, animated: true) {
// completion goes here
}
}
Make UIAlertController+AlertController Category as:
UIAlertController+AlertController.h
typedef void (^UIAlertCompletionBlock) (UIAlertController *alertViewController, NSInteger buttonIndex);
#interface UIAlertController (AlertController)
+ (instancetype)showAlertIn:(UIViewController *)controller
WithTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSString *)otherButtonTitle
tapBlock:(UIAlertCompletionBlock)tapBlock;
#end
UIAlertController+AlertController.m
#implementation UIAlertController (NTAlertController)
+ (instancetype)showAlertIn:(UIViewController *)controller
WithTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSString *)otherButtonTitle
tapBlock:(UIAlertCompletionBlock)tapBlock {
UIAlertController *alertController = [self alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
if(cancelButtonTitle != nil) {
UIAlertAction *cancelButton = [UIAlertAction
actionWithTitle:cancelButtonTitle
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
tapBlock(alertController, ALERTACTION_CANCEL); // CANCEL BUTTON CALL BACK ACTION
}];
[alertController addAction:cancelButton];
}
if(otherButtonTitle != nil) {
UIAlertAction *otherButton = [UIAlertAction
actionWithTitle:otherButtonTitle
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
tapBlock(alertController, ALERTACTION_OTHER); // OTHER BUTTON CALL BACK ACTION
}];
[alertController addAction:otherButton];
}
[controller presentViewController:alertController animated:YES completion:nil];
return alertController;
}
#end
in your ViewController.m
[UIAlertController showAlertIn:self WithTitle:#"" message:#"" cancelButtonTitle:#"Cancel" otherButtonTitles:#"Other" tapBlock:^(UIAlertController *alertController, NSInteger index){
if(index == ALERTACTION_CANCEL){
// CANCEL BUTTON ACTION
}else
if(index == ALERTACTION_OTHER){
// OTHER BUTTON ACTION
}
[alertController dismissViewControllerAnimated:YES completion:nil];
}];
NOTE: If you want to add more than two buttons then add another
more UIAlertAction to the UIAlertController.
Use UIAlertController instead of UIAlertView
-(void)showMessage:(NSString*)message withTitle:(NSString *)title
{
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//do something when click button
}];
[alert addAction:okAction];
UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
[vc presentViewController:alert animated:YES completion:nil];
}
I tried the above methods, and no one can show the alert view, only when I put the presentViewController: method in a dispatch_async sentence:
dispatch_async(dispatch_get_main_queue(), ^ {
[self presentViewController:alert animated:YES completion:nil];
});
Refer to Alternative to UIAlertView for iOS 9?.
-(void)showAlert{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#"Title"
message:#"Message"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
[self showAlert]; // calling Method
Check this:
UIAlertController *alertctrl =[UIAlertController alertControllerWithTitle:#"choose Image" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *camera =[UIAlertAction actionWithTitle:#"camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self Action]; //call Action need to perform
}];
[alertctrl addAction:camera];
-(void)Action
{
}
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:#"Are you sure you want to logout?"
message:#""
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:#"Logout"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action)
{
}];
UIAlertAction* noButton = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle no, thanks button
}];
[alert addAction:noButton];
[alert addAction:yesButton];
[self presentViewController:alert animated:YES completion:nil];

Ios uialertcontroller uiimagepicker dismiss : app crash

I have created a new app under xcode 6 and an old cold no more work.
Since iOS 8 we use UIalertcontroller to show action sheet
I use it to lauch photolibrary and select a picture but when I want to dismiss the picker, my app crash and I don't know why.
Below my code:
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
UIAlertController * uiViewActionSheet= [UIAlertController
alertControllerWithTitle:#"Action"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* chooseExisting = [UIAlertAction
actionWithTitle:NSLocalizedString(#"CHOOSE_EXISTING",#"Choose Existing")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do some thing here
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
[uiViewActionSheet dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
[uiViewActionSheet dismissViewControllerAnimated:YES completion:nil];
}];
[uiViewActionSheet addAction:chooseExisting];
[uiViewActionSheet addAction:cancel];
[self presentViewController:uiViewActionSheet animated:YES completion:nil];
Below the imagepicker function :
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.uImageProfile.image = chosenImage;
self.lstatutImage.text = #"save";
[picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];
if(picker.sourceType == UIImagePickerControllerSourceTypeCamera)
{
UIImageWriteToSavedPhotosAlbum(chosenImage, nil, nil, nil);
}
__block NSString* resultSaveImage = #"";
//Save image onthe server
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
resultSaveImage = [controllerObject saveProfileImage:self.uImageProfile.image];
dispatch_async(dispatch_get_main_queue(), ^{
if(![resultSaveImage isEqualToString:#"\"ok\""])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"ERROR",#"Error") message:resultSaveImage delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
});
});
}
My code crash here :
[picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];
Have you any idea ?
You're dismissing the picker, and then calling it:
[picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];
if(picker.sourceType == UIImagePickerControllerSourceTypeCamera) {...
You also don't need the [uiViewActionSheet dismissViewControllerAnimated:YES completion:nil]; lines!
Try moving the dismiss line to the end. If it still crashes, it might be that the presentingViewController is not alive anymore.

Warning message after actionSheet buttons are pressed

I am very new to iOS development and have encountered an error that I just can't seem to find a solution for. I have searched for solutions everywhere, but maybe it is my newness that is preventing me from seeing the problem.
The exact warning that is printed in the log is:
Attempt to dismiss from view controller <_UIAlertShimPresentingViewController: 0x7aaa4b90> while a presentation or dismiss is in progress!
It occurs right after I touch a button on the actionSheet.
Here is the code:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
if (buttonIndex == 0) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
}
else if (buttonIndex == 1) {
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}
else if (buttonIndex == 2) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:picker animated:YES completion:nil];
}
} else {
if (buttonIndex == 0) {
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:NO completion:NULL];
}
else if (buttonIndex == 1) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:picker animated:YES completion:NULL];
}
}
}
The implementation of the actionSheet, I have the IBAction connected a toolbar button located an the .xib file.
- (IBAction)addImage:(id)sender {
UIActionSheet *popUpSheet = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles: nil];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[popUpSheet addButtonWithTitle:#"Camera"];
[popUpSheet addButtonWithTitle:#"Photo Library"];
[popUpSheet addButtonWithTitle:#"Camera Roll"];
[popUpSheet addButtonWithTitle:#"Cancel"];
popUpSheet.cancelButtonIndex = popUpSheet.numberOfButtons-1;
} else {
[popUpSheet addButtonWithTitle:#"Photo Library"];
[popUpSheet addButtonWithTitle:#"Camera Roll"];
[popUpSheet addButtonWithTitle:#"Cancel"];
popUpSheet.cancelButtonIndex = popUpSheet.numberOfButtons-1;
}
[popUpSheet showFromBarButtonItem: self.toolbarItems[0] animated:YES]; }
Everything has been delegated correctly from what I can tell:
DetailViewController.m
#interface DetailViewController () < UINavigationControllerDelegate, UIImagePickerControllerDelegate >
DetailViewController.h
#interface DetailViewController : UIViewController <UIActionSheetDelegate>
Any insight would be greatly appreciated and extremely helpful.
Your code looks correct. Try using the:
actionSheet:didDismissWithButtonIndex:
method. It's sent to the delegate after the animation ends. Hope it helps.
Finally figured out the problem. The compiler was giving me this warning because Apple recently combined UIActionsheet and UIAlert into one controller type called UIAlertController. Used the new UIAlertController and my problem was solved.
- (IBAction)addImage:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
UIAlertController *popUpSheet = [UIAlertController alertControllerWithTitle:nil message:#"Select your Choice" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *photoLibrary = [UIAlertAction actionWithTitle:#"Photo Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
NSLog(#"Photo Library was touched");
[popUpSheet dismissViewControllerAnimated:YES completion: nil];
}];
UIAlertAction *recentPhotos = [UIAlertAction actionWithTitle:#"Camera Roll" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:picker animated:YES completion:nil];
NSLog(#"Camera Roll was touched");
[popUpSheet dismissViewControllerAnimated:YES completion: nil];
}];
UIAlertAction *camera = [UIAlertAction actionWithTitle:#"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
NSLog(#"Camera was touched");
[popUpSheet dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[popUpSheet dismissViewControllerAnimated:YES completion:nil];
}];
[popUpSheet addAction:photoLibrary];
[popUpSheet addAction:recentPhotos];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[popUpSheet addAction:camera];
}
[popUpSheet addAction:cancel];
[self presentViewController:popUpSheet animated:YES completion:nil];
}

Resources