I'm selecting an image in UIImagePickerController.
After the image was selected I'm firing another UIActionSheet to select from
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
_selected_image = info[UIImagePickerControllerEditedImage];
UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:#"" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:
#"Share this photo",
nil];
popup.tag = 2;
[popup showInView:[UIApplication sharedApplication].keyWindow];
}
When the user selects "Share" a long process is starting.
I want the MBProgressHUD to show during the process. I can't get progress HUD to show before stuff are starting to happen.
Tried two things:
- (void)actionSheet:(UIActionSheet *)popup clickedButtonAtIndex:(NSInteger)buttonIndex {
if (popup.tag == 1)
{
...
}
else if (popup.tag == 2)
{
if (buttonIndex == 1) //cancel
{
_selected_image = nil;
}
else if (buttonIndex == 0)
{
[imagePicker dismissViewControllerAnimated:YES completion:NULL];
[self doSharePhoto];
return;
}
[imagePicker dismissViewControllerAnimated:YES completion:NULL];
}
}
doSharePhotos start running when the imagePicker is still showing.
Tried:
- (void)actionSheet:(UIActionSheet *)popup clickedButtonAtIndex:(NSInteger)buttonIndex {
if (popup.tag == 1)
{
}
else if (popup.tag == 2)
{
if (buttonIndex == 1) //cancel
{
_selected_image = nil;
}
else if (buttonIndex == 0)
{
[imagePicker dismissViewControllerAnimated:YES completion:^{
[self doSharePhoto];
}];
return;
}
[imagePicker dismissViewControllerAnimated:YES completion:NULL];
}
}
and in this case the MBProgressHUD inside doSharePhoto doesn't apprear at all
** EDIT **:
The code that starts the HUD is in [self doSharePhoto]:
- (void)doSharePhoto {
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
HUD.labelText = #"Please wait...";
HUD.delegate = self;
...
}
Any thoughts?
Does your doSharePhoto method perform the long running operation inside a seperate thread? If not it's blocking the main thread so , MBProgressHUD doesn't get a chance to show. Your doSharePhoto must look like this,
- (void)doSharePhoto {
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// Do the long running operation here...
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
});
});
}
EDIT: Problem might be with the view you add your HUD to,
Change
self.navigationController.view to
self.navigationController.visibleViewController.view
So,
[MBProgressHUD hideHUDForView:self.navigationController.visibleViewController.view animated:YES];
Related
So, similar to this question:
UIAlertView taking time to show
I'm running into a problem where my UIAlertView is not displaying until after all of my other code has run.
I have tried using the
dispatch_async(dispatch_get_main_queue(),^{
[self presentLoadingAlertWithTitle:NSLocalizedString(#"Creating Account...", nil)];
});
But I was unable to get it to change what happens. Currently, a user clicks a button and it triggers createAccountButtonReleased. Then it seems to skip over the dispatch_async part, run the rest of the code, and then come back to it.
- (IBAction)createAccountButtonReleased:(id)sender {
dispatch_async(dispatch_get_main_queue(),^{
[self presentLoadingAlertWithTitle:NSLocalizedString(#"Creating Account...", nil)];
});
if ((![self.firstNameTextField hasText] && ![self.lastNameTextField hasText]) || ![self.emailTextField hasText] || ![self.passwordTextField hasText]) {
[self dismissAlert:^(void){ [self presentOkAlertWithTitle:NSLocalizedString(#"An error has occured.", nil) andMessage:NSLocalizedString(#"All text fields should be filled out.", nil)];}];
}
else if (([self.firstNameTextField hasText] || [self.lastNameTextField hasText]) && [self.emailTextField hasText] && [self.passwordTextField hasText]) {
UserCreateWebService *ucws = [[UserCreateWebService alloc]initWithFirstName:self.firstNameTextField.text andLastName:self.lastNameTextField.text andEmail:self.emailTextField.text andPassword:self.passwordTextField.text];
NSString *ucwsResponse = [ucws userCreated];
if ([ucwsResponse isEqualToString:#"Contact already exists"]) {
[self dismissAlert:^(void){[self presentOkAlertWithTitle:NSLocalizedString(#"An error has occured.", nil) andMessage:NSLocalizedString(#"A contact with that information already exists. Please enter a different email and try again.", nil)];}];
}
else if (ucwsResponse.length > 75 && [[ucwsResponse substringToIndex:75] isEqualToString:#"EX: The specified string is not in the form required for an e-mail address."]) {
[self dismissAlert:^(void){ [self presentOkAlertWithTitle:NSLocalizedString(#"An error has occured.", nil) andMessage:NSLocalizedString(#"The entered email is not in the required format. Please enter as:\"username#domain.com\"", #"Replace username with the equivalent word and domain with the equivalent word")];}];
}
else if ([ucwsResponse isEqualToString:#"True"]) {
//logs in the newly created user
User *user = [[[UserLoginService alloc]initWithUsername:self.emailTextField.text andPassword:self.passwordTextField.text] attemptLogin];
if (!user) {
[self dismissAlert:^(void){[self presentOkAlertWithTitle:NSLocalizedString(#"Something went wrong.", nil) andMessage:NSLocalizedString(#"Please ensure that you have an internet connection.", nil)];}];
}
else {
//post a notification
}
[self dismissAlert];
UIAlertView *success = [[UIAlertView alloc]initWithTitle:NSLocalizedString(#"Registration Complete", nil) message:NSLocalizedString(#"Welcome! Press the Ok button to get started!" , nil) delegate:self cancelButtonTitle:NSLocalizedString(#"Ok", nil) otherButtonTitles:nil, nil]; // Makes Alert View
[success show]; //shows alert view
}
else { //need to beef up the error message with the return from the json.
[self presentOkAlertWithTitle:NSLocalizedString(#"An error has occured.", nil) andMessage:NSLocalizedString(#"Your account has not been created. Please check your internet connection or try again later.", nil)];
}
}
presentLoadingAlertWithTitle:
- (void)presentLoadingAlertWithTitle:(NSString *)title completion:(void (^)(void))completion
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:#"\n" preferredStyle:UIAlertControllerStyleAlert];
CGRect activityIndicatorFrame = alertController.view.bounds;
activityIndicatorFrame.size.height -= 35.0f;
activityIndicatorFrame.origin.y += 35.0f;
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:activityIndicatorFrame];
[activityIndicator setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
[activityIndicator setUserInteractionEnabled:NO];
[activityIndicator startAnimating];
[alertController.view addSubview:activityIndicator];
[self presentViewController:alertController animated:YES completion:completion];
}
- (void)presentLoadingAlertWithTitle:(NSString *)title
{
[self presentLoadingAlertWithTitle:title completion:nil];
}
Any advice?
I am working on universal app and want to open camera. Now I am able to open camera with UIimagePickerController on iphone but on iPad its not working. I have searched for solution and found this code
self.cameraController = [[UIImagePickerController alloc] init];
self.cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;
self.cameraController.modalPresentationStyle = UIModalPresentationCurrentContext;
self.cameraController.showsCameraControls = NO;
self.cameraController.navigationBarHidden = YES;
self.cameraController.wantsFullScreenLayout = NO;
[self.cameraController setCameraOverlayView:ar_overlayView];
[ar_overlayView setFrame:self.cameraController.view.bounds];
UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
if([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0 && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self presentViewController:self.cameraController animated:YES completion:nil];
}];
}
else{
[vc presentViewController:self.cameraController animated:YES completion:nil];
}
Also followed some tutorial like
TechnoTopia Post but didn't find any luck.I have tested it in iphone 5s and is working fine in it but on iPad mini the camera is not presenting. Any help would be appreciated!
Try This For iPad
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
UIImagePickerController* picker=[[UIImagePickerController alloc]init];
picker.delegate=self;
picker.allowsEditing=YES;
picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}];
}
else if (buttonIndex==1)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
UIImagePickerController* picker=[[UIImagePickerController alloc]init];
picker.delegate=self;
picker.allowsEditing=YES;
picker.sourceType=UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];}];
}
}
For iPad you should do:
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:picker];
[popover presentPopoverFromRect:self.selectedImageView.bounds inView:self.selectedImageView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
self.popOver = popover;
}else {
[self presentModalViewController:picker animated:YES];
}
where in .h file declare:
#property (nonatomic, strong) UIPopoverController *popOver;
You should handle popover delegate:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
Apple docs:
apple link
it states that:
"Present the user interface by calling the presentViewController:animated:completion: method of the currently active view controller, passing your configured image picker controller as the new view controller. On iPad, present the user interface using a popover. Doing so is valid only if the sourceType property of the image picker controller is set to UIImagePickerControllerSourceTypeCamera."
try this code :
UIImagePickerController *aImgPickerController = [[UIImagePickerController alloc] init];
aImgPickerController.delegate = self;
aImgPickerController.allowsEditing = YES;
aImgPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self presentViewController:aImgPickerController animated:NO completion:^{
NSLog(#"success present picker");
}];
});
- (IBAction)onClickSelectPicture:(id)sender
{
if( [UIImagePickerController isCameraDeviceAvailable: UIImagePickerControllerCameraDeviceFront ])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType=UIImagePickerControllerSourceTypeCamera;
picker.cameraDevice=UIImagePickerControllerCameraDeviceFront;
[self presentViewController:picker animated:YES completion:NULL];
}
else
{
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Device has no camera"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[myAlertView show];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
imgProf = info[UIImagePickerControllerEditedImage];
[viewNewPhoto setImage:imgProf];
imgProf = [self imageWithImage:imgProf scaledToSize:CGSizeMake(imgProf.size.width/4, imgProf.size.height/4)];
if (imgProf == nil)
{
imgPhotoString = #"NoPhoto";
}else
{
imgPhotoString = [self getStringFromImage:imgProf];
}
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Is anyone else having an issue with UIImagePickerController in iOS 8? The method below works perfectly well in iOS 7 on an iPad, but I get the following error when I run this in XCode 6 (Beta 3 or 4) when I try to present the picker (last line). If it matters, the selection of the sourceType is coming from an alertView that is presented in the same place.
Warning: Attempt to present <UIImagePickerController: 0x7c0ae400> on <CAGUCreateContactViewController: 0x7bf61a00> which is already presenting (null)
Method to open imagePicker.
- (void)openPhotoPicker:(UIImagePickerControllerSourceType)sourceType
{
if ([UIImagePickerController isSourceTypeAvailable:sourceType]) {
NSArray *availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
if ([availableMediaTypes containsObject:(NSString *)kUTTypeImage]) {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationFullScreen;
imagePickerController.sourceType = sourceType;
imagePickerController.mediaTypes = #[(NSString *)kUTTypeImage];
imagePickerController.delegate = self;
self.imagePickerController = imagePickerController;
if (sourceType == UIImagePickerControllerSourceTypeCamera) {
[self presentViewController:self.imagePickerController animated:YES completion:nil];
} else {
if (self.popoverVC) {
[self.popoverVC dismissPopoverAnimated:YES];
self.popoverVC = nil;
}
self.popoverVC = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];
[self.popoverVC presentPopoverFromRect:self.nameAndPicCell.picture.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
}
}
}
I think this is because in iOS 8, alert views and action sheets are actually presented view controllers (UIAlertController). So, if you're presenting a new view controller in response to an action from the UIAlertView, it's being presented while the UIAlertController is being dismissed. I worked around this by delaying the presentation of the UIImagePickerController until the next iteration of the runloop, by doing this:
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self openPhotoPicker:sourceType];
}];
However, the proper way to fix this is to use the new UIAlertController API on iOS 8 (i.e. use if ([UIAlertController class]) ... to test for it). This is just a workaround if you can't use the new API yet.
I agree with Ben Lings issue detection. I would suggest a simpler solution in case when using UIActionSheet. I simply moved my code that reacts on Action Sheet selection from:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
{
// my code
}
into:
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex; // after animation
{
// my code
}
This way app is guarantied that code will be executed AFTER UIActionSheet animation finishes.
Since UIAlertView has similar delegate method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex; // after animation
{
// my code
}
I suppose that similar solution may apply.
Here is a solution that worked for me
if([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self presentViewController:cameraUI animated:NO completion:nil];
}];
}
else{
[controller presentViewController:cameraUI animated:NO completion:nil];
}
Remember to alloc cameraUI
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
Build and Go!
I was facing the same problem in iOS 8.
Then I saw the change log of the latest update to iOS i.e. 8.0.2 on the device.
It is mentioned in this update that_
"Fixes an issue that prevented some apps from accessing photos from Photo Library"
So test your app using XCode 6 on device with iOS 8.0.2 version it will work fine
Don't test it on iOS 8.0 simulator.
This helped me, hope the same for you.
UIImagePickerController *imagePickerController= [[UIImagePickerController alloc] init];
[imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
// image picker needs a delegate so we can respond to its messages
[imagePickerController setDelegate:self];
self.shouldCallViewWillAppear = NO;
if(IS_IOS8)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// Place image picker on the screen
[self presentViewController:imagePickerController animated:YES completion:nil];
}];
}
else
{
[self presentViewController:imagePickerController animated:YES completion:nil];
}
You can dismiss the presented view controller (if any) by using
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
This worked for me.
All you need to do is dismiss already presented ViewController:
if (self.presentedViewController) {
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
}
[self openPhotoPicker:sourceType];
If it still produces error, put openPhotoPicker: to completion handler
I simply did this:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,
(unsigned long)NULL), ^(void) {
[self retractActivePopover];
dispatch_async(dispatch_get_main_queue(), ^ {
_activePopover=imagePickerPopover;
UIBarButtonItem *callingButton = (UIBarButtonItem*) sender;
[imagePickerPopover presentPopoverFromBarButtonItem:callingButton permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
});
});
On iOS 8 you should use the new API:
if (SYSTEM_VERSION_IOS_8) {
self.imagePickerController.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController *popPC = self.imagePickerController.popoverPresentationController;
popPC.barButtonItem = self.popoverItem;
popPC.permittedArrowDirections = UIPopoverArrowDirectionAny;
[self presentViewController:self.imagePickerController animated:YES completion:nil]
}
I recommend you watch the 2014 WWDC session 228 a look in side presentation controllers
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 :)
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;
}
performSelector:withObject:afterDelay solved my problem.
also didDismissWithButtonIndex do the trick.
Max
Here's a Xamarin solution. What worked for me was to add my actions to a Dismissed event handler.
this.btnPhoto.TouchUpInside += (sender, e) =>
{
actionSheet = new UIActionSheet ("Add Photo");
actionSheet.AddButton ("Take Photo");
actionSheet.AddButton ("Select from Library");
actionSheet.AddButton ("Cancel");
actionSheet.DestructiveButtonIndex = -1; // red
actionSheet.CancelButtonIndex = 3; // black
actionSheet.Clicked += delegate(object a, UIButtonEventArgs b)
{
actionSheet.Dismissed += (object aSender, UIButtonEventArgs dismissArgs) =>
{
switch (dismissArgs.ButtonIndex)
{
case 0:
showCamera ();
break;
case 1:
showPhotoLibrary ();
break;
}
};
};
actionSheet.ShowInView (view);
};
I have a button to capture picture then it save to photoAlbum. I used another button for load image from using imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; then i implemented didFinishPickingMediaWithInfo: for loading photo album to newImage. My need is after capture photo i have alertview then click YES button automatically load last captured image PhotoAlbum and add newImage. Is it possible?
- (void)saveImageToPhotoAlbum
{
[self.library saveImage:[self stillImage] toAlbum:#"Album" withCompletionBlock:^(NSError *error) {
if (error!=nil) {
NSLog(#"Big error: %#", [error description]);
}
}];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Gallery"
message:#"Do you want to use the captured image?" delegate:self
cancelButtonTitle:#"Cancel" otherButtonTitles:#"Yes", nil];
alertView.tag = 2;
[alertView show];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//set image
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
newImage = [[UIImageView alloc] initWithImage:[info objectForKey:UIImagePickerControllerOriginalImage]];
[newImage setFrame:CGRectMake(0, 0, 320, 568)];
[self.view addSubView:newImage];
}
I need to display last captured image to newImage click of alertView button:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 1){
}
}
Before use below code take UIImage *img in .h file
- (void)saveImageToPhotoAlbum
{
[self.library saveImage:[self stillImage] toAlbum:#"Album" withCompletionBlock:^(NSError *error) {
if (error!=nil) {
NSLog(#"Big error: %#", [error description]);
}
}];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Gallery"
message:#"Do you want to use the captured image?" delegate:self
cancelButtonTitle:#"Cancel" otherButtonTitles:#"Yes", nil];
alertView.tag = 2;
[alertView show];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//set image
img=nil;
img=[info objectForKey:UIImagePickerControllerOriginalImage];
[self saveImageToPhotoAlbum];
// [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if(newImage)
{
[newImage removeFromSuperview];
newImage=nil;
}
newImage = [[UIImageView alloc] initWithImage:img];
[newImage setFrame:CGRectMake(0, 0, 320, 568)];
[self.view addSubView:newImage];
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
else
{
img=nil;
[picker dismissViewControllerAnimated:YES completion:nil];
}
}
I have a UIActionSheet for iPad which has three options :
Cancel
Camera
Photo Library
When I touch the "Photo Library" option I get a crash and a message
UIStatusBarStyleBlackTranslucent is not available on this device.
I read this post, but didn't figure it out.
Can someone help me?
Update :
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
{
imgController = [[UIImagePickerController alloc] init];
imgController.allowsEditing = YES;
imgController.sourceType = UIImagePickerControllerSourceTypeCamera;
imgController.delegate=self;
[self presentModalViewController:imgController animated:YES];
}
else if (buttonIndex == 1)
{
imgController = [[UIImagePickerController alloc] init];
imgController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imgController.delegate=self;
[self presentModalViewController:imgController animated:YES];
}
}
I get crash in last line i.e [self presentModalViewController:imgController animated:YES];
For iPad it is recommended that you should use popover to present the MediaBrowser (camera / photoLibrary):
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
UIPopoverController *popOverController = [[UIPopoverController alloc] initWithContentViewController:ipc];
popOverController.delegate = self;
You can also set the content view for popover:
ipc.delegate = self;
ipc.editing = NO;
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
ipc.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:ipc.sourceType];
[popOverController presentPopoverFromRect:btnGallery.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Try below code its works for me perfect
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex
{
if(buttonIndex==0)
{
[self takePhoto];
}
else if(buttonIndex==1)
{
[self choosePhoto];
}
}
-(void)takePhoto
{
UIDevice *device = [UIDevice currentDevice];
NSString *currDevice = [device model];
NSLog(#"device is %#",currDevice);
if(![currDevice isEqualToString:#"iPhone Simulator"])
{
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
UIImagePickerController *imgPickerCon = [[UIImagePickerController alloc] init];
imgPickerCon.sourceType = UIImagePickerControllerSourceTypeCamera;
imgPickerCon.delegate = self;
[self presentModalViewController:imgPickerCon animated:YES];
[imgPickerCon release];
imgPickerCon = nil;
}
else{
UIAlertView *alrt=[[UIAlertView alloc] initWithTitle:#"Message" message:#"Camera Will Not Open in Simulator" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alrt show];
[alrt release];
}
}
-(void)choosePhoto
{
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
UIImagePickerController *imgPickerCon = [[UIImagePickerController alloc] init];
imgPickerCon.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imgPickerCon.delegate = self;
[self presentModalViewController:imgPickerCon animated:YES];
[imgPickerCon release];
imgPickerCon = nil;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)myimage editingInfo:(NSDictionary *)editingInfo
{
[picker dismissModalViewControllerAnimated:YES];
image=myimage;
imgView.image=myimage;
}
Try removing your status bar settings from the plist file all together and adding the following to your AppDelegate's applicationDidFinishLaunchingWithOptions:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackTranslucent];
}
UPDATE:
Try this
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex)
{
case 0:
{
imgController = [[UIImagePickerController alloc] init];
imgController.allowsEditing = YES;
imgController.sourceType = UIImagePickerControllerSourceTypeCamera;
imgController.delegate=self;
[self presentModalViewController:imgController animated:YES];
}
case 1:
{
imgController = [[UIImagePickerController alloc] init];
imgController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imgController.delegate=self;
[self presentModalViewController:imgController animated:YES];
}
}
}
A little late but is the UIViewController whats calling presentModalViewController:animated: a child of a UIPopoverController? If so, thats whats causing this. Try calling it from the popovers parentViewController
Just as you pointed out with the post that you have read, the simple solution would be adding a row in the plist with the following key-value
UIStatusBarStyle~ipad | String | UIStatusBarStyleBlackOpaque
(3rd row in the picture here, sorry cause I can't post image yet now)
This is one of the solution if you don't want to do too much "dirty work" on the codes there, just leave it to the plist to get the job done.
But if you don't mind writing codes, the solution given by VSN will do the same as my suggestion.