如何从手机图库中获取照片?

Posted

技术标签:

【中文标题】如何从手机图库中获取照片?【英文标题】:How do I get a photo from the phone gallery? 【发布时间】:2013-07-16 09:51:17 【问题描述】:

我在尝试打开图像选择器时遇到了一个大问题,但不知道为什么我的应用程序在此行崩溃

[self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];

我在许多应用程序中都使用了这段代码,代码运行良好,但在我当前的应用程序中,这段代码给出了首选界面方向的异常。

使用的代码:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
if (cameraAvailable == TRUE) 
    if (buttonIndex == 0) 
        imgpkrController.delegate = self;
        imgpkrController.allowsEditing = YES;
        imgpkrController.sourceType = UIImagePickerControllerSourceTypeCamera;
        //[self presentModalViewController:(UIViewController *)imgpkrController animated:YES];
        [self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];
    
    else if(buttonIndex == 1)
        imgpkrController.delegate = self;
        imgpkrController.allowsEditing = YES;
        imgpkrController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //[self presentModalViewController:(UIViewController *)imgpkrController animated:YES];
        [self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];
    

if (buttonIndex == 0) 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
    
            imgpkrController.delegate = self;
            imgpkrController.allowsEditing = YES;
            imgpkrController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            //imgpkrController.modalPresentationStyle=UIModalPresentationFullScreen;
            //[self presentModalViewController:imgpkrController animated:YES];
            [self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];
            //[self presentViewController:imgpkrController animated:YES completion:nil];
    
    else
        NSLog(@"ipad photo");
        imgpkrController.delegate = self;
        popoverController = [[UIPopoverController alloc] initWithContentViewController:imgpkrController];
        [popoverController presentPopoverFromRect:CGRectMake(260.0, 0.0, 400.0, 570.0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    

else
    if (buttonIndex == 0) 
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
        
            imgpkrController.delegate = self;
            imgpkrController.allowsEditing = YES;
            imgpkrController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            //[self presentModalViewController:(UIViewController *)imgpkrController animated:YES];
            [self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];
        else
             NSLog(@"ipad photo");
            imgpkrController.delegate = self;
            popoverController = [[UIPopoverController alloc] initWithContentViewController:imgpkrController];
            [popoverController presentPopoverFromRect:CGRectMake(260.0, 0.0, 400.0, 570.0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        
    

这行代码崩溃

[self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];

例外:

2013-07-16 15:11:02.143 HMW[1335:c07] *** Terminating app due to uncaught exception     'UIApplicationInvalidInterfaceOrientation', reason:     'preferredInterfaceOrientationForPresentation must return a supported interface orientation!'
*** First throw call stack:
(0x2ec5012 0x2a7ce7e 0x2ec4deb 0x17a54da 0x19a444c 0x17a24f3 0x17a2777 0x2a663 0x1a952c1 0x2a90705 0x16bb920 0x16bb8b8 0x177c671 0x177cbcf 0x177bd38 0x16eb33f 0x16eb552 0x16c93aa 0x16bacf8 0x38a7df9 0x38a7ad0 0x2e3abf5 0x2e3a962 0x2e6bbb6 0x2e6af44 0x2e6ae1b 0x38a67e3 0x38a6668 0x16b865c 0x2802 0x2735)
libc++abi.dylib: terminate called throwing an exception

【问题讨论】:

您的shouldAutorotate 返回什么?你的preferredInterfaceOrientationForPresentation 会返回什么? @JoachimIsaksson 我应该自动旋转返回“是”,而且我没有在我的应用程序中设置任何首选界面方向。 如果您有shouldAutorotate 返回YES,您需要(至少对于某些控件)设置首选方向。考虑到消息'preferredInterfaceOrientationForPresentation must return a supported interface orientation!' 我怀疑你正在攻击其中一个。 @JoachimIsaksson 那么我到底需要做什么。 @prem 的link 下面应该对您有所帮助,您需要从方向查询方法中返回一个有效值。 【参考方案1】:

不确定是什么导致了您的错误消息,因为它似乎与设备方向有关,但我设法使用以下代码使其工作:

- (IBAction)attachPhotosClicked:(id)sender

    if (![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear])
            
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [_popoverController presentPopoverFromRect:self.attachButton.frame inView:self.view
                          permittedArrowDirections:(UIPopoverArrowDirectionLeft)
                                          animated:YES];
        return;
    
    else
            
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose a source" 
                                                                 delegate:self  
                                                        cancelButtonTitle:@"Cancel" 
                                                   destructiveButtonTitle:nil 
                                                        otherButtonTitles:@"Camera", @"Photo Library", nil];
        [actionSheet showFromRect:self.attachButton.frame inView:self.view animated:YES];
    

【讨论】:

【参考方案2】:
- (IBAction)selectPhoto:(UIButton *)sender 

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

[self presentViewController:picker animated:YES completion:NULL];

【讨论】:

【参考方案3】:

您会在 ios 6.0 中发现此问题。看看下面的链接,

UIPopoverController orientation crash in iOS 6

应该会在以后的版本中修复。

【讨论】:

【参考方案4】:

我认为您的问题是由于 iOs 版本问题。在 iOs 6 中更改了 interfaceOrientation 协议。 我认为这个链接可以帮助你。 preferredInterfaceOrientationForPresentation must return a supported interface orientation?

【讨论】:

以上是关于如何从手机图库中获取照片?的主要内容,如果未能解决你的问题,请参考以下文章

我如何使用目标 c 中的照片框架从图库中访问照片

当我在 swift 3 中从图库中获取照片时,为啥收藏视图为空?

iOS 应用程序 - 尝试从图库中获取 iOS 照片但出现错误

如何从图库中隐藏照片并将其存储在我的应用程序中

Android:从图库加载的位图在 ImageView 中旋转

图库照片不见了怎么恢复?