在横向模式下打开UIImagePickerController

3

我希望在我的应用程序中打开 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个回答

4
您无法这样做。根据文档的说法:

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

这需要更多的工作,但是您可以使用AssetsLibrary框架来访问图像列表并创建自己的图像选择器。

1
这篇文章展示了如何使用Assets Library创建一个图片选择器:http://www.icodeblog.com/2010/10/07/cloning-uiimagepickercontroller-using-the-assets-library-framework/ - Caroline
但我需要你的另外帮助。 - Ronak
我遇到了以下错误信息:"OBJC_CLASS$_ALAssetsLibrary",引用自: uploadfromGallery.o中的ALAssetsLibrary objc-class-ref-to-ALAssetsLibrary ld: 找不到符号 collect2: ld返回1个退出状态 - 请帮助我解决。 - Ronak
@Ronak:您需要将AssetsLibrary框架添加到您的项目中,这可以通过与添加其他现有框架相同的方式来完成。 - Anomie
@Ronak:请注意,显示选择器的方法可能不受支持。文档只提到presentModalViewController:animated:UIPopoverController作为呈现它的方法。 - Anomie
显示剩余3条评论

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

我已经尝试了一些代码添加

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

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上如此)。

有人成功地将视频录像机限制为横向模式吗?


1
我使用了以下代码:
[self.view addSubview:imgPicker.view]; 
[imgPicker viewWillAppear:YES]; 
[imgPicker viewDidAppear:YES];

不要使用presentModalViewController方法,而是使用以下代码:[self presentViewController:picker animated:NO completion:nil];


请注意,显示选择器的方法可能不受支持。文档只提到了 presentModalViewController:animated: 或 UIPopoverController 作为呈现它的方法。 - Anomie
@ Anomie,我有一个问题要问你,使用上面的代码,图像选择器在横屏模式下加载良好,但我遇到一个问题。如果我向上或向下滚动,我的应用程序会崩溃。我得到了错误:*** Terminating app due to uncaught exception 'NSGenericException',reason:'*** Collection <__NSArrayM: 0x69959d0> was mutated while being enumerated. 我花了将近5个小时,但是还没有解决它。所以,你能帮我一下吗……… - Ronak

0

来自UIImagePickerController类参考

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接