【问题标题】:Not able to show the Image in proper format from camera in landscape mode无法在横向模式下从相机以正确的格式显示图像
【发布时间】:2013-03-11 21:53:59
【问题描述】:

我正在 Ios6 中开发 iPad 应用程序,当我们单击右侧栏按钮时,我正在执行如下操作:

-(IBAction)camerabuttonAction:(id)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.delegate = self;

   self.popoverController = [[UIPopoverController alloc] initWithContentViewController:picker];
   [self.popoverController presentPopoverFromRect:CGRectMake(50, -250, 500, 300) inView:appDelegate.splitview.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}

我的问题是,当我处于景观模式时,如果我单击按钮。每次使用后相机以纵向模式显示(图像以反向模式显示)。但是,如果我摇动 iPad,它就会在 LandScape 中显示,即方向正确。

请看下面的图片

当我处于风景模式时,如果我点击按钮,相机会显示如下图像:

如果我摇晃 iPad,相机会显示如下图像:

我已经尝试了很多并用谷歌搜索,但我没有找到任何解决方案。它正在消磨我的时间,所以如果有人对此进行了研究,请指导我并发布示例代码。

【问题讨论】:

  • 您在哪个版本的 iOS6 上看到这个?我无法在 iOS6.1.2/ipadMini 或 iOS6.1.3/ipadMini 上重现此行为。
  • [picker willAnimateRotationToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation duration:0];尝试添加此代码
  • 或者尝试将您的 UIImagePickerController 添加到 UINavigationController。然后将您的导航控制器添加到弹出控制器并再次检查。
  • 我们都在等待你的回应,哥们.. 没有什么帮助你:(?@Harini Goutham

标签: iphone ios ipad ios-simulator uiimagepickercontroller


【解决方案1】:

我在我的项目中遇到了同样的问题,我尝试了以下方法并为我工作

我的代码:

- (UIImage *)scaleAndRotateImage:(UIImage *)image {
    int kMaxResolution = 640; // Or whatever

    CGImageRef imgRef = image.CGImage;

    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);


    CGAffineTransform transform = CGAffineTransformIdentity;
    CGRect bounds = CGRectMake(0, 0, width, height);
    if (width > kMaxResolution || height > kMaxResolution) {
        CGFloat ratio = width/height;
        if (ratio > 1) {
            bounds.size.width = kMaxResolution;
            bounds.size.height = roundf(bounds.size.width / ratio);
        }
        else {
            bounds.size.height = kMaxResolution;
            bounds.size.width = roundf(bounds.size.height * ratio);
        }
    }

    CGFloat scaleRatio = bounds.size.width / width;
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
    CGFloat boundHeight;
    UIImageOrientation orient = image.imageOrientation;
    switch(orient) {

        case UIImageOrientationUp: //EXIF = 1
            transform = CGAffineTransformIdentity;
            break;

        case UIImageOrientationUpMirrored: //EXIF = 2
            transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            break;

        case UIImageOrientationDown: //EXIF = 3
            transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;

        case UIImageOrientationDownMirrored: //EXIF = 4
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
            transform = CGAffineTransformScale(transform, 1.0, -1.0);
            break;

        case UIImageOrientationLeftMirrored: //EXIF = 5
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;

        case UIImageOrientationLeft: //EXIF = 6
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;

        case UIImageOrientationRightMirrored: //EXIF = 7
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeScale(-1.0, 1.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;

        case UIImageOrientationRight: //EXIF = 8
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;

        default:
            [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];

    }

    UIGraphicsBeginImageContext(bounds.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
        CGContextScaleCTM(context, -scaleRatio, scaleRatio);
        CGContextTranslateCTM(context, -height, 0);
    }
    else {
        CGContextScaleCTM(context, scaleRatio, -scaleRatio);
        CGContextTranslateCTM(context, 0, -height);
    }

    CGContextConcatCTM(context, transform);

    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return imageCopy;
}

【讨论】:

    【解决方案2】:

    您可以尝试对您的 imagePickerController 进行转换

    imagePickerController.view.transform = CGAffineTransformMakeRotation(-M_PI/2);
    

    【讨论】:

    • 即使我也面临同样的问题。这个转换代码真的很有帮助。
    • 工作。不过使用 M_PI_2,而不是 M_PI/2。
    【解决方案3】:

    我在 Google 搜索中研究了您的问题,并得到了一些结果或可能性,如下所示:-

    结果 1

    一些答案​​说它是一个 iOS 6 错误,所以你不能像下面的问题一样修复它:-

    iPad camera popover preview wrong rotation and scale

    结果 2

    我不认为我们可以控制相机的方向。相机方向属性是内置的,它会随着设备的方向而变化。

    iPad camera orientation portrait mode?

    结果 3

    您可以在中心管理缩放的实时 iPhone 相机视图,以物理移动选取器框架。通过波纹管代码:-

    [picker.view setFrame:CGRectMake(xOffset,yOffset,picker.view.frame.size.width,picker.view.frame.size.height)];
    

    希望你得到真正的解决方案。关于这个错误或问题

    【讨论】:

    • 实际上我的方向问题是......当我转动 Ipad 时,相机显示方向错误..但是在检测到面部之后..它进入正确的方向..@nitin
    • 检查这个 ios6 oriation :-stackoverflow.com/questions/12650137/…
    【解决方案4】:

    我将在这里尝试一个很好的猜测,因为我自己还没有这样做......

    您是否尝试过创建 UIImagePickerController 的子类并从 ViewController(它是超类)实现 interfaceOrientation 的方法?

    【讨论】:

      【解决方案5】:

      请试试这个

      [popController presentPopoverFromRect:CGRectMake(1024, 
        self.view.bounds.origin.y + (self.view.bounds.size.height / 2), 1, 1) 
      inView:self.view 
      permittedArrowDirections:UIPopoverArrowDirectionRight 
      animated:YES];
      

      【讨论】:

        【解决方案6】:

        我有一个只有 ipad 应用程序的横向模式,我吓坏了,因为我阅读了所有这些帖子,但我使用这个非常简单的代码拍照,它非常适合我唯一的横向应用程序。重要的是,我不会从库中选择图像,我只是拍摄照片并将其放在其他图像上,它非常适合我。

        if ([UIImagePickerController isSourceTypeAvailable:
                 UIImagePickerControllerSourceTypeCamera])
            {
        
                UIImagePickerController *imagePicker =
                [[UIImagePickerController alloc] init];
                imagePicker.delegate = self;
                imagePicker.sourceType =
                UIImagePickerControllerSourceTypeCamera;
                imagePicker.mediaTypes = [NSArray arrayWithObjects:
                                          (NSString *) kUTTypeImage,
                                          nil];
                imagePicker.allowsEditing = NO;
                [self presentViewController:imagePicker
                                        animated:YES completion:nil];
               // [imagePicker release];
                newMedia = YES;
            }
        
        -(void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingMediaWithInfo:(NSDictionary *)info
        {
            [self.popoverController dismissPopoverAnimated:true];
          //  [popoverController release];
        
            NSString *mediaType = [info
                                   objectForKey:UIImagePickerControllerMediaType];
            [self dismissViewControllerAnimated:YES completion:nil];
            if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
                UIImage *image = [info
                                  objectForKey:UIImagePickerControllerOriginalImage];
        
                imageView.image = image;
                if (newMedia)
                    UIImageWriteToSavedPhotosAlbum(image,
                                                   self,
                                                   @selector(image:finishedSavingWithError:contextInfo:),
                                                   nil);
            }
            else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
            {
                // Code here to support video if enabled
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-06
          • 2015-08-17
          • 2017-09-16
          • 1970-01-01
          • 2013-06-01
          • 2017-07-15
          相关资源
          最近更新 更多