如何在iPhone上打开图库以选择图像?

如何在iPhone上打开图库以选择图像?,iphone,ios,Iphone,Ios,我需要知道如何在iPhone SDK中打开图库。我相信我的代码是正确的,但我仍然得到一个例外 这是我的代码: -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ imgpkrController.delegate = self; imgpkrController.allowsEditing = YES; imgpkr

我需要知道如何在iPhone SDK中打开图库。我相信我的代码是正确的,但我仍然得到一个例外

这是我的代码:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
        imgpkrController.delegate = self;
        imgpkrController.allowsEditing = YES;
        imgpkrController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //[self presentModalViewController:(UIViewController *)imgpkrController animated:YES];

        [self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];}
通过使用此代码,am将获得异常:

UIApplicationInvalidInterfaceOrientation', reason:
  preferredInterfaceOrientationForPresentation must return a supported interface orientation!
所以我尝试了以下代码:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
        UIInterfaceOrientation interface = [[UIApplication sharedApplication] statusBarOrientation];
        if(interface == UIDeviceOrientationPortrait)
        {
            imgpkrController.delegate = self;
            imgpkrController.allowsEditing = YES;
            imgpkrController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            //[self presentModalViewController:(UIViewController *)imgpkrController animated:YES];

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

        }else if (interface ==UIDeviceOrientationLandscapeLeft || interface ==UIDeviceOrientationLandscapeRight)
        {
            imgpkrController.delegate = self;
            imgpkrController.allowsEditing = YES;
            imgpkrController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            //[self presentModalViewController:(UIViewController *)imgpkrController animated:YES];
            [self presentViewController:(UIViewController *)imgpkrController animated:YES completion:nil];}}

但这也给出了同样的例外。有人能帮我解决这个问题吗?

我认为您之前使用的代码没有任何问题。为此,请继续使用该代码。我认为UIInterfaceOrientation中的代码有问题。您可以尝试转到Target->summy->,然后尝试从那里处理方向

如果这没有帮助,那么您需要在以下链接中学习UIInterfaceOrientation


Ankit我试着按照你说的方式去做,但没有成功。是否有人可以帮助我解决此问题。imgpkrController应该已经定义为视图控制器为什么要在当前模型视图控制器中添加imgpkrController,如(UIViewController*)imgpkrController。因为这是我知道的唯一方法。如果有其他方法,请与我分享。这段代码在其他项目上运行得很好,但在这个项目上却不行。