【问题标题】:UIImagePickerController supports portrait mode only. Any alternatives that support landscape mode?UIImagePickerController 仅支持纵向模式。支持横向模式的任何替代方案?
【发布时间】:2012-12-30 04:47:25
【问题描述】:

iOS 文档说“UIImagePickerController 类仅支持纵向模式”。如果我想以横向模式显示照片库,还有其他选择吗?

【问题讨论】:

  • 这是重复的:以及其他一千个问题
  • 其实我认为这个问题比其他“重复”要好得多。其他问题是在一两年前针对 iOS 3 或 4 提出的,人们讨论了许多不适合今天的替代方案;现在是 iOS 6,所以最好再次提出这个问题,让情况更清楚,更具体到最新的操作系统。

标签: iphone orientation uiimagepickercontroller


【解决方案1】:

这可能会对您有所帮助。您可以使用 ALAssetsLibrary 和资产类来获取设备中的图片,您可以使用它们以横向模式和纵向模式显示,就像 uiimagepicker 一样。

- (void)viewDidLoad
{
    [super viewDidLoad];
    [activity startAnimating];


    appObj=(ImagePickerAppDelegate *)[[UIApplication sharedApplication]delegate];

    void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
    {
        if(result != NULL) 
        {
            //assets is a mutualable array...for storing the images that are in the device..
            [assets addObject:result];
        }
    };

    void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) =  ^(ALAssetsGroup *group, BOOL *stop) 
    {
        if(group != nil)
        {
            [group enumerateAssetsUsingBlock:assetEnumerator];
        }
        //meth is a user defined method..   
        [self meth];
        [activity stopAnimating];
        [activity setHidden:YES];
    };
    assets = [[NSMutableArray alloc] init];
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:assetGroupEnumerator 
                         failureBlock: ^(NSError *error) { NSLog(@"Failure");}];
}


-(void)meth
{
    NSLog(@"%i",[assets count]);

    if(userOrientation==UIInterfaceOrientationPortrait || userOrientation==UIInterfaceOrientationPortraitUpsideDown)
    {
        NSLog(@"haii");
        [scrollView removeFromSuperview];

        scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
        scrollView.backgroundColor=[UIColor whiteColor];

        NSLog(@"%i",[assets count]);
        for (int i = 0; i < [assets count]; i++) 
        {
            imgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [imgBtn setFrame:CGRectMake((i%4*80)+2,(i/4*80)+2,75,75)];
            imgBtn.tag=i;
            [imgBtn addTarget:self action:@selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];
            ALAsset *asset=[assets objectAtIndex:i];
            [imgBtn setImage:[UIImage imageWithCGImage:[asset thumbnail]] forState:UIControlStateNormal];
            [scrollView addSubview:imgBtn];
        }
        scrollView.contentSize = CGSizeMake(320,(([assets count]/4)+1)*300 );
    }

    if(userOrientation==UIInterfaceOrientationLandscapeRight || userOrientation==UIInterfaceOrientationLandscapeLeft)
    {
        [scrollView removeFromSuperview];
        scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 480,320)];
        for (int i = 0; i < [assets count]; i++) 
        {
            imgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [imgBtn setFrame:CGRectMake((i%6*80)+2,(i/6*80)+2,75,75)];
            imgBtn.tag=i;
            [imgBtn addTarget:self action:@selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];
            ALAsset *asset=[assets objectAtIndex:i];
            [imgBtn setImage:[UIImage imageWithCGImage:[asset thumbnail]] forState:UIControlStateNormal];
            [scrollView addSubview:imgBtn];
        }
        scrollView.contentSize = CGSizeMake(480,(([assets count]/4)+1)*300);
    }
    [self.view addSubview:scrollView];
}



-(void)imageClicked:(UIButton *)sender
{
    //for picking the images that the user has selected we are using other array "selectedImages" i.e declared in the app delegate
    ALAsset *asset=[assets objectAtIndex:sender.tag];
    [appObj.selectedImages addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
    NSLog(@"%i",[appObj.selectedImages count]);
    [self.navigationController popViewControllerAnimated:YES ];
}

【讨论】:

    【解决方案2】:

    我没有检查这是否非法,但它对我有用。如果您希望 UIImagePickerController 在横向代码中启动(并保持):

    //Initialize picker
    
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
       picker.delegate = self;
    
    
    //set Device to Landscape. This will give you a warning. I ignored it.
    //warning: 'UIDevice' may not respond to '-setOrientation:'
    
    
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
    
    //Set Notifications so that when user rotates phone, the orientation is reset to landscape.
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    
    //Refer to the method didRotate:   
    [[NSNotificationCenter defaultCenter] addObserver:self
                  selector:@selector(didRotate:)
                   name:@"UIDeviceOrientationDidChangeNotification" object:nil];
    
    //Set the picker source as the camera   
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    //Bring in the picker view   
    [self presentModalViewController:picker animated:YES];
    

    方法didRotate:

    - (void) didRotate:(NSNotification *)notification
    
    {
          //Maintain the camera in Landscape orientation
     [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 1970-01-01
      • 2018-10-11
      • 2011-05-16
      相关资源
      最近更新 更多