Reputation: 2413
I have an universal app for both iPhone and iPad. Using below code I am trying to get the picture from photo library or from camera. For iphone its working fine but for iPad it does not show anything when I click on any button from the alert view. What can be the issue?
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
if (buttonIndex == 0) {
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = YES;
picker.delegate = self;
camera = NO;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
} else if (buttonIndex == 1) {
BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
camera = YES;
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = YES;
picker.delegate = self;
picker.sourceType = hasCamera ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
}
}else {
// We are using an iPad
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
popoverController=[[UIPopoverController alloc] initWithContentViewController:imagePickerController];
popoverController.delegate=self;
[popoverController presentPopoverFromRect:[self.view bounds] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (clickedButton == YES) {
UIImage *originalImage, *editedImage;
editedImage = (UIImage *) [info objectForKey: UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey: UIImagePickerControllerOriginalImage];
if (editedImage) {
thisImage = editedImage;
} else {
thisImage = originalImage;
}
imageThis.image = thisImage;
NSData *imageData = UIImageJPEGRepresentation(thisImage, 30);
[defaults setObject:imageData forKey:@"thisImage"];
[defaults synchronize];
// [[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(thisImage) forKey:@"thisImage"];
}
else {
UIImage *originalImage, *editedImage;
editedImage = (UIImage *) [info objectForKey: UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey: UIImagePickerControllerOriginalImage];
if (editedImage) {
thatImage = editedImage;
} else {
thatImage = originalImage;
}
imageThat.image = thatImage;
NSData *imageData = UIImageJPEGRepresentation(thatImage, 30);
[defaults setObject:imageData forKey:@"thatImage"];
[defaults synchronize];
//[[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(thatImage) forKey:@"thatImage"];
}
[picker dismissModalViewControllerAnimated:YES];
}
Upvotes: 0
Views: 1117
Reputation: 21
That is because you use UIActionSheet on iPad. On iPad, UIActionSheet uses UIPopoverController to present the sheet, and in the UIActionSheetDelegate methods you use another UIPopoverController when the first UIPopoverController used by UIActionsheet has not been hidden. All you need to know is that at any time there only can be one UIPopoverController shown in the screen.
Upvotes: 1
Reputation: 857
It's this line:
[popoverController presentPopoverFromRect:[self.view bounds] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
You need to present from a rectangle that is within your controller's view (I'm assuming that self
is a view controller), or else you get weird results. In testing just now, I had one popover refuse to appear and one that appeared with the tip of its arrow centered on screen. So for instance:
[popoverController presentPopoverFromRect:[myButtonOutlet frame] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Upvotes: 0