我有适用于iPhone和iPad的通用应用程序。使用下面的代码我试图从照片库或相机中获取图片。对于iphone它的工作正常,但对于iPad,当我点击警报视图中的任何按钮时,它不会显示任何内容。可能是什么问题?
-(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];
}
答案 0 :(得分:1)
那是因为你在iPad上使用UIActionSheet。在iPad上,UIActionSheet使用UIPopoverController呈现工作表,而在UIActionSheetDelegate方法中,当UIActionsheet使用的第一个UIPopoverController未被隐藏时,您使用另一个UIPopoverController。您需要知道的是,任何时候屏幕上都只能显示一个UIPopoverController。
答案 1 :(得分:0)
就是这一行:
[popoverController presentPopoverFromRect:[self.view bounds] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
您需要从控制器视图中的矩形出现(我假设self
是视图控制器),否则您会得到奇怪的结果。刚刚进行测试时,我有一个弹出窗口拒绝出现,其中一个弹出窗口以箭头为中心出现在屏幕上。例如:
[popoverController presentPopoverFromRect:[myButtonOutlet frame] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];