【问题标题】:How to force UIImagePickerController to take images in Landscape mode only....?如何强制 UIImagePickerController 仅在横向模式下拍摄图像......?
【发布时间】:2013-08-02 10:27:08
【问题描述】:

我想在 UIImagePickerController 上添加一个自定义叠加视图,这个叠加视图只支持横向模式。我只希望 UIImagePickerController 在横向模式下拍照。有什么办法可以做到这一点...?

【问题讨论】:

标签: iphone ios objective-c uiimagepickercontroller


【解决方案1】:

我用两个不同的方法创建了两个不同的叠加层,调用与方向匹配的那个。

- (IBAction)takePicture:(id)sender {

[[UIDevice currentDevice]beginGeneratingDeviceOrientationNotifications];

if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)) {


    [self portraitDisplayOverlay];

}else{

    [self startCameraControllerFromViewController:self usingDelegate:self];


   }
}

在纵向叠加层上,我将 UIImagePickerController 属性设置为隐藏相机控件

picker.showCameraControls = NO;

此叠加层只是指示用户使用横向模式,当它倾斜到横向时调用正确的叠加层并瞧。

希望这会有所帮助,吉姆。

【讨论】:

    【解决方案2】:

    请阅读说明 UIImagePickerController 类仅支持纵向模式的 Apple 文档:Click Here

    请注意,如果您在横向模式下调用 UIImagePickerController,您的应用可能会被 Apple 拒绝

    您必须找到另一种方法,例如使用 AVFoundation 框架创建自定义相机

    试试这个方法 将您拍摄的图像传递给这样的方法

    // Code from: http://discussions.apple.com/thread.jspa?messageID=7949889
    - (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;
    }
    

    【讨论】:

    • 我还创建了一个示例,其中相机在横向模式下打开并且仅在横向模式下拍摄图片
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    相关资源
    最近更新 更多