无法在横屏模式下正确显示相机拍摄的图片

10

我正在iOS 6上开发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,则它会以正确的方向显示,即横屏。

请参见下面的图片

当我处于横屏模式并点击按钮时,相机显示如下所示的图像:

enter image description here

如果我摇动iPad,则相机显示以下图像:

enter image description here

我已经尝试了很多并搜索了谷歌,但我没有找到任何解决方案。这浪费了我的时间,所以如果有人已经处理过它,请指导我并发布示例代码。


你看到的是哪个确切版本的iOS6?我无法在iOS6.1.2 / ipadMini或iOS6.1.3 / ipadMini上重现此行为。 - foundry
https://dev59.com/vGnWa4cB1Zd3GeqP1YV_#13763016 - Bala
[picker willAnimateRotationToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation duration:0]; 尝试添加这段代码。 - Dinesh Raja
或者尝试将您的UIImagePickerController添加到UINavigationController中。然后将您的navigationController添加到pop over控制器中并再次检查。 - Dinesh Raja
我们都在等待你的回复,伙计.. 什么都没有帮到你吗 :( ? @Harini Goutham - Bala
6个回答

10
我在项目中遇到了相同的问题,我尝试了下面的方法并且对我有效。 我的代码:
- (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;
}

9
您可以尝试对您的imagePickerController进行变换。
imagePickerController.view.transform = CGAffineTransformMakeRotation(-M_PI/2);

1
即使我也遇到了同样的问题。这个转换代码确实很有帮助。 - Swati
1
使用M_PI_2而不是M_PI/2,已经可以正常工作了。 - tjklemz

2

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

结果1

有些答案说这是iOS 6的一个bug,所以您无法像下面的问题那样修复它:

iPad相机弹出窗口预览旋转和缩放错误

结果2

我认为我们无法控制相机的方向。相机方向属性是内置的,会随设备的方向而改变。

iPad相机方向纵向模式?

结果3

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

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

希望您能得到实际的解决方案。关于此错误或问题。


实际上,我的方向问题是...当我旋转iPad时,相机显示的方向不正确。但是在检测到脸部后,它会进入正确的方向..@nitin - Goutham
请查看以下关于iOS6方向的内容:https://dev59.com/jWcs5IYBdhLWcg3wrmDC - Nitin Gohel

1

我有一个仅支持横屏模式的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
    }
}

1
我将尝试一个好的猜测,因为我自己没有做过这个...

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

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

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