您好我正在开发通用应用程序(iPhone / iPad)。一个功能是我必须从相册中选择一张照片并在UIImageView上显示。
现在的问题是它在iPhone上运行良好但是当我尝试打开相册时它会崩溃。我在行动表委托中的代码是这样的:
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
if ( ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]))
{
if (buttonIndex == 0)
{
[self lockAllImagesOnTheScreen];
imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imagePicker animated:YES];
}
if (buttonIndex == 1)
{
[self lockAllImagesOnTheScreen];
imagePicker.sourceType= UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
}
}
else {
if (buttonIndex == 0)
{
[self lockAllImagesOnTheScreen];
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
imagePicker.sourceType= UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
}
}
}
else{
if ( ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]))
{
if (buttonIndex == 0)
{
[self lockAllImagesOnTheScreen];
imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imagePicker animated:YES];
}
if (buttonIndex == 1)
{
[self lockAllImagesOnTheScreen];
imagePicker.sourceType= UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
}
}
else {
if (buttonIndex == 0)
{
[self lockAllImagesOnTheScreen];
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
imagePicker.sourceType= UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
}
}
}
}
任何人都可以帮助我吗?我已经检查了stackOverflow并且也用Google搜索了但是徒劳无功。
答案 0 :(得分:57)
UIImagePickerController
必须出现UIPopoverController
。
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];
}
编辑:为UIPopoverController
添加强大的属性:
@property (nonatomic, strong) UIPopoverController *popOver;
应该在委托方法中解除popover:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
答案 1 :(得分:6)
在这里,我向您展示SWIFT方式:
import UIKit
class StoreItemViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate
{
@IBOutlet weak var button: UIButton!
@IBOutlet weak var productImage: UIImageView!
var popOver:UIPopoverController?
@IBAction func buttonSelected(sender:UIButton)
{
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum)
{
var imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
imagePickerController.allowsEditing = false
if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad
{
self.popOver = UIPopoverController(contentViewController: imagePickerController)
self.popOver?.presentPopoverFromRect(self.productImage.bounds, inView: self.productImage, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
}
else
{
self.presentViewController(imagePickerController, animated: true, completion: { imageP in
})
}
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
//do anything with the image
let selectedImage = info[UIImagePickerControllerOriginalImage] as UIImage
//closing the popup
popOver?.dismissPopoverAnimated(true)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController)
{
println("cancel")
//closing the popup
popOver?.dismissPopoverAnimated(true)
}
}
答案 2 :(得分:4)
Apple文档说
“通过调用来呈现用户界面 presentViewController:animated:completion:当前的方法 活动视图控制器,传递您配置的图像选择器 控制器作为新的视图控制器。在iPad上,呈现用户 使用popover的界面。这样做仅在sourceType时有效 图像选择器控制器的属性设置为 UIImagePickerControllerSourceTypeCamera“。
这与它的表现完全相反?!?您无法从弹出窗口中显示UIImagePickerControllerSourceTypeCamera
,并且您无法以模态方式显示UIImagePickerControllerSourceTypePhotoLibrary
和UIImagePickerControllerSourceTypeSavedPhotosAlbum
。
...奇怪
答案 3 :(得分:3)
POST iOS 8: 尝试在
中添加popOver控制器[[NSOperationQueue mainQueue] addOperationWithBlock:^ { }];
原因: 这是因为在iOS 8中,警报视图和操作表实际上是视图控制器(UIAlertController)。因此,如果您为了响应来自UIAlertView的动作而呈现新的视图控制器,则在UIAlertController被解除时它将被呈现。您需要在主队列上执行此操作而不会干扰导航。