以横向模式打开UIImagePickerController

时间:2011-04-20 23:03:49

标签: ios iphone

我想在我的应用程序中打开iphone保存的图像。我的应用程序在横向模式下工作当我尝试使用presentModalViewController方法从iphone库加载所有保存的照片时,它将以纵向模式打开。我希望在横向模式下。这是代码:

picker = [[UIImagePickerController alloc] init];
picker.delegate = self;

picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
picker.allowsEditing = NO;
picker.navigationBarHidden = YES;
picker.wantsFullScreenLayout = YES;
[picker setNavigationBarHidden:TRUE];

[self presentModalViewController:picker animated:NO];
NSLog(@"%@", self.view.subviews);
[picker release];  

任何人都可以帮助我.. 提前谢谢。

6 个答案:

答案 0 :(得分:5)

你做不到。 Quoth the documentation

  

UIImagePickerController类仅支持纵向模式。此类旨在按原样使用,不支持子类化。

这是一项工作,但您可以使用AssetsLibrary framework访问图片列表并创建自己的图片选择器。

答案 1 :(得分:2)

我们无法在风景中获得UIImagePickerController ......

但是我们可以将设备中的图像放到一个数组中,并以横向模式和纵向模式显示....它看起来像UIImagePickerController ......

我们应该为此使用ALAsset类和ALAssetsLibrary ..

void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
{
    if(result != NULL) 
    {
        [assets addObject:result];
    }
};

void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) =  ^(ALAssetsGroup *group, BOOL *stop) 
{
    if(group != nil)
    {
        [group enumerateAssetsUsingBlock:assetEnumerator];
    }
    [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");}];

在viewDidLoad

-(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];
 }

答案 2 :(得分:2)

我尝试过添加一些代码

[self.navigationController setNavigationBarHidden:YES];
[self addChildViewController:picker];
[self.view addSubview:picker.view];
picker.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
picker.view.frame = self.view.bounds;
[picker didMoveToParentViewController:self];


(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
   UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
   self.imageView.image = chosenImage;
   [picker.view removeFromSuperview];
   [picker removeFromParentViewController];
}


(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{
   [picker.view removeFromSuperview];
   [picker removeFromParentViewController];
}

答案 3 :(得分:1)

我使用了以下代码:

[self.view addSubview:imgPicker.view]; 
[imgPicker viewWillAppear:YES]; 
[imgPicker viewDidAppear:YES];

而不是使用presentModalViewController方法, [self presentModalViewController:picker animated:NO];

答案 4 :(得分:1)

我尝试了这种方法,同时更密切地关注API文档:

- (void)openEmbeddedImagePicker:(UIImagePickerController *)picker {
    [self.navigationController setNavigationBarHidden:YES];
    [self addChildViewController:picker];
    [self.view addSubview:picker.view];
    picker.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    picker.view.frame = self.view.bounds;
    [picker didMoveToParentViewController:self];
}

但录像机的取消和播放按钮没有响应。此外,虽然包含VC(上述方法中的self)将方向限制为横向,但在两种横向模式之间旋转会混淆拾取器覆盖条的布局(至少在iOS 6上)。

是否有人成功将录像机限制为横向模式?

答案 5 :(得分:0)

来自UIImagePickerController Class Reference

  

重要事项: UIImagePickerController类仅支持纵向模式。此类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改,但有一个例外。您可以为cameraOverlayView属性分配自定义视图,并使用该视图显示其他信息或管理相机界面与代码之间的交互。