UIIMagePicker 相机横向方向问题

Posted

技术标签:

【中文标题】UIIMagePicker 相机横向方向问题【英文标题】:UIIMagePicker camera landscape orientation issue 【发布时间】:2013-12-24 05:40:26 【问题描述】:

我有一个视图控制器,并且我已将 UIImagePicker 添加为子视图。在将设备方向从纵向更改为横向时,相机似乎是半屏而不是全屏。相机中的图像也旋转了 90 度。我在我的应用程序中支持设备方向。如何解决此相机方向问题。任何帮助表示赞赏。

//展示相机的代码

- (id)init

    self = [super init];
    if (self) 

        UIImagePickerController * picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType =  UIImagePickerControllerSourceTypeCamera ;
        picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
        picker.allowsEditing = YES;
        self.cameraPicker = picker;
        cameraPicker.view.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height );;
        //cameraPicker.view.frame = CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height );

        [self.view addSubview:cameraPicker.view];


    

    return self;

//支持设备方向的代码

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

    [self setFramesForControls:toInterfaceOrientation];


-(void)setFramesForControls :(UIInterfaceOrientation)orientation


    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
    
        cameraPicker.view.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height );

    
    else if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
    
        cameraPicker.view.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height );
    

【问题讨论】:

【参考方案1】:

试试这个效果很好

在 viewdidload 方法中声明

    picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType =  UIImagePickerControllerSourceTypeCamera ;
    picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    picker.allowsEditing = YES;

在按钮单击事件上执行以下操作

[self presentModalViewController:picker animated:YES];

无论检查设备方向;

【讨论】:

嗨,我试过了,但不幸的是,这并不能解决相机中的方向问题。【参考方案2】:

为了防止您的图像,此代码将帮助您-

在 ViewController 类的顶部定义这些宏 -

 #define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

然后以这种方式管理您的委托方法-

#pragma mark- Delegate methods of UIImagePickerController Class

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info


 UIImage *pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];

  NSString *imgType =  [info objectForKey:UIImagePickerControllerReferenceURL];

    //NSLog(@"%@",imgType);

    imgType = [imgType lastPathComponent];

    NSData *imageData;

    if (SYSTEM_VERSION_LESS_THAN(@"6.0"))
    
        // code here
         imageData = UIImagePNGRepresentation(pickedImage);
    

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0"))
    
        // code here
          imageData = UIImageJPEGRepresentation(pickedImage, 0.5);
    


    NSError * error = nil;

    [imageData writeToFile:path options:NSDataWritingAtomic error:&error];

    if (error != nil)
    
        NSLog(@"ERROR: %@", error);
        return;
    

 [picker dismissViewControllerAnimated:YES completion: nil];




它将根据您的要求捕获图像。

【讨论】:

您好,我的问题是,当我从相机中的横向返回时,视图控制器并未更改为横向,但仍保持纵向模式。如何解决这个问题。 似乎是自动调整大小问题,请告诉我更多细节吗? 我没有使用 xib,只是创建 imagePicker 并将其添加到视图控制器中,如我的编码中所述。

以上是关于UIIMagePicker 相机横向方向问题的主要内容,如果未能解决你的问题,请参考以下文章

更改 UIImagePicker 纵横比

Wikitude - 横向模式下相机方向的问题

使用 Ionic / ngCordova 将相机/屏幕方向锁定为横向

UIImagePicker 拍照时旋转错误

横向模式下的 UIImagePicker 控制器

uiimagepicker 用于显示相机