UIIMagePicker相机横向方向问题

时间:2013-12-24 05:40:27

标签: ios iphone uiinterfaceorientation

我有一个视图控制器,我已将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 );
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个工作正常

在viewdidload方法中声明这个

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

按钮点击事件执行以下操作

[self presentModalViewController:picker animated:YES];

无论是检查设备方向;

答案 1 :(得分:0)

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

在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];



}

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