26

iOS6 で完全に動作する UIPopoverController 内で UIImagePickerController を使用しています。iOS 7 では、画像をキャプチャするために表示される「プレビュー」画像が回転しますが、写真を撮ると正しく保存されます。

これが私のピッカーを取得する方法です:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
                              (NSString *) kUTTypeImage,
                              nil];
imagePicker.allowsEditing = NO;

そして、それをポップオーバー コントローラーに追加します。

self.imagePickerPopOver = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
    [self.imagePickerPopOver presentPopoverFromRect:CGRectMake(aPosViewA.x, cameraButton_y, 100.0, 30.0) inView:self.detailViewController.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

これらは、正しい位置にポップオーバーを表示するための UIScrollView でのボタン位置の計算です。

presentPopoverFromRect:CGRectMake(aPosViewA.x, cameraButton_y, 100.0, 30.0)

いくつかの組み合わせを試したので、そこに問題があるとは思いません。

フルスクリーン モードでも画像をキャプチャしようとしましたが、アプリは横向きモードしか使用できません。画像が縦向きモードで撮影され、モーダル ビューが閉じられた場合、アプリも縦向きモードのままになります。UIImagePickerController がポートレート モードに切り替わらないようにする方法や、モーダル ビューが閉じられた場合にアプリを強制的にランドスケープ モードに戻す方法が見つかりませんでした。

アップデート

私はここから答えを得て、さらに一歩前進しました。

ピッカーを作成した後、ポップオーバーを表示する前にビューを変換します。

switch ([UIApplication sharedApplication].statusBarOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
            self.imagePicker.view.transform = CGAffineTransformMakeRotation(M_PI/2);
            break;
        case UIInterfaceOrientationLandscapeRight:
            self.imagePicker.view.transform = CGAffineTransformMakeRotation(-M_PI/2);
            break;
        default:
            break;
    }

iPadの向きを変えない限り、これは機能します。そのために、オリエンテーション変更イベントに登録しています。

[[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:)  name:UIDeviceOrientationDidChangeNotification  object:nil];

ピッカー ビューを変更します。

- (void)orientationChanged:(NSNotification *)notification{

    if (self.imagePicker) {
        switch ([UIApplication sharedApplication].statusBarOrientation) {
            case UIInterfaceOrientationLandscapeLeft:
                self.imagePicker.view.transform = CGAffineTransformMakeRotation(M_PI/2);
                break;
            case UIInterfaceOrientationLandscapeRight:
                self.imagePicker.view.transform = CGAffineTransformMakeRotation(-M_PI/2);
                break;
            default:
                break;
        }
    }
}

残りの問題:最初に書いたように、写真を撮ったときに、それを受け入れるか却下するかが正しく表示されました。これも変形するようになりました。どういうわけか、画像がいつ撮影されたかを知り、元に戻す必要があります。

そして、これは本当に厄介なハックであり、おそらく次の iOS アップデートでは機能しません。それをよりクリーンな方法で実装する方法を知っている人はいますか?

更新 2

これは厄介すぎました。問題を解決するよりクリーンなソリューションを見つけましたが、ポップオーバー コントローラーのイメージピッカーに関する最初の質問に対する回答ではありません。これは Apple では推奨されていませんが、許可されています。

UIImagePickerController を次のようにサブクラス化しました。

@implementation QPImagePickerController

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}

@end

ポップオーバーではなくフルスクリーンでイメージピッカーを使用しています。これまでのところ iOS7 でテスト済みです。

4

4 に答える 4

14

UIImagePickerController には cameraViewTransform というプロパティがあります。これに CGAffineTransform を適用すると、プレビュー画像は変換されますが、キャプチャされた画像は変換されないため、正しくキャプチャされます。私はあなたが説明したのと同じ問題を抱えており、次のようにカメラコントローラーを作成してポップオーバーに配置することで解決しました(iOS7の場合):

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

[imagePickerController setDelegate:self];

imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

CGFloat scaleFactor=1.3f;

switch ([UIApplication sharedApplication].statusBarOrientation) {

        case UIInterfaceOrientationLandscapeLeft:

            imagePickerController.cameraViewTransform = CGAffineTransformScale(CGAffineTransformMakeRotation(M_PI * 90 / 180.0), scaleFactor, scaleFactor);

            break;

        case UIInterfaceOrientationLandscapeRight:

            imagePickerController.cameraViewTransform = CGAffineTransformScale(CGAffineTransformMakeRotation(M_PI * -90 / 180.0), scaleFactor, scaleFactor);

            break;

        case UIInterfaceOrientationPortraitUpsideDown:

            imagePickerController.cameraViewTransform = CGAffineTransformMakeRotation(M_PI * 180 / 180.0);

            break;

            default:
                break;
        }

 __popoverController = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];

[__popoverController presentPopoverFromRect:presentationRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

また、ランドスケープのときは画像を拡大縮小して、ビューファインダーを他の場合よりもいっぱいにするようにします。私の考えでは、これはかなり厄介ですが、iOS7.1 が到着したら削除できることを願っています。

于 2013-09-28T21:40:48.430 に答える
3

Journeyman が提供した回答に基づいて、UIIMagePickerView が表示されている間にデバイスが回転する場合を処理する別のソリューションを見つけました。また、ビューが UIOrientationLandscapeRight/UIOrientationLandscapeLeft から UIOrientationPortrait に回転する場合も処理します。

UIImagePickerController のサブクラスを作成しました。

#import <UIKit/UIKit.h>

@interface PMImagePickerController : UIImagePickerController

@end

次に、デバイスの向きが変わった場合に通知を受け取るように登録しました。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fixCameraOrientation:) name:UIDeviceOrientationDidChangeNotification object:nil];

セレクター fixCameraOrientation には、sourceType がカメラであることを確認するチェックでラップされた、1 つの追加のケースを含む Journeyman のコードが含まれています。

- (void)fixCameraOrientation:(NSNotification*)notification
{
    if (self.sourceType == UIImagePickerControllerSourceTypeCamera) {
        CGFloat scaleFactor=1.3f;

        switch ([UIApplication sharedApplication].statusBarOrientation) {
            case UIInterfaceOrientationLandscapeLeft:
                self.cameraViewTransform = CGAffineTransformScale(CGAffineTransformMakeRotation(M_PI * 90 / 180.0), scaleFactor, scaleFactor);
                break;


            case UIInterfaceOrientationLandscapeRight:
                self.cameraViewTransform = CGAffineTransformScale(CGAffineTransformMakeRotation(M_PI * -90 / 180.0), scaleFactor, scaleFactor);
                break;

            case UIInterfaceOrientationPortraitUpsideDown:
                self.cameraViewTransform = CGAffineTransformMakeRotation(M_PI * 180 / 180.0);
                break;

            case UIInterfaceOrientationPortrait:
                self.cameraViewTransform = CGAffineTransformIdentity;
                break;

            default:
                break;
        }
    }

}

ここで重要なケースは、デバイスの向きが縦向きになるケースです。この場合、オーバーレイのビューをリセットする必要があります。また、デバイスが回転した場合に備えて、イメージ ピッカー ビューの読み込み後に fixCameraOrientation セレクターを呼び出すことも重要です。

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self fixCameraOrientation:nil];
}
于 2014-02-27T14:28:30.207 に答える
2

私のアプリでも同様の状況がありました。ただし、プレビューは iOS8 ではなく iOS7 で正しく回転していました。このコードは、複数のオリエンテーションがあることを前提としています。

まず、サブクラス化しUIImagePickerControllerます。

#import <AVFoundation/AVFoundation.h>上から始めて、.m ファイルに追加します。

また、初期方向を保存するプロパティと、@property (nonatomic) UIInterfaceOrientation startingOrientation;クリッピングを削除する条件用のプロパティを追加します@property (nonatomic) BOOL didAttemptToRemoveCropping;

いくつかの通知を聞くつもりでした。 UIApplicationDidChangeStatusBarOrientationNotification明らかに、デバイスの回転をリッスンします。AVCaptureSessionDidStartRunningNotificationカメラがキャプチャを開始するとすぐに呼び出されます。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationDidChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(captureSessionDidStart:) name:AVCaptureSessionDidStartRunningNotification object:nil];

-captureSessionDidStart:ビューが実際に画面上にあることを確認し、カメラが表示されることになっていることを確認する条件を追加しますif (self.view.window && self.sourceType == UIImagePickerControllerSourceTypeCamera)。その場合は、開始方向を設定しself.startingOrientation = [UIApplication sharedApplication].statusBarOrientation;ます。

上記と同じ条件を追加し-statusBarOrientationDidChange:ますが、今回は true の場合、カメラ トランスフォームを更新します。最初に、初期回転に基づいてオフセット回転を取得します。UIImagePickerControllerこれは、縦向き以外の向きで入力するときに必要です。

CGFloat startingRotation = ({
    CGFloat rotation;

    switch (self.startingOrientation) {
        case UIInterfaceOrientationPortraitUpsideDown:
            rotation = M_PI;
            break;
        case UIInterfaceOrientationLandscapeLeft:
            rotation = -M_PI_2;
            break;
        case UIInterfaceOrientationLandscapeRight:
            rotation = M_PI_2;
            break;
        default:
            rotation = 0.0f;
            break;
    }

    rotation;
});

次に、カメラの変換を現在の回転で更新します。

self.cameraViewTransform = CGAffineTransformMakeRotation(({
    CGFloat angle;

    switch ([UIApplication sharedApplication].statusBarOrientation) {
        case UIInterfaceOrientationPortraitUpsideDown:
            angle = startingRotation + M_PI;
            break;
        case UIInterfaceOrientationLandscapeLeft:
            angle = startingRotation + M_PI_2;
            break;
        case UIInterfaceOrientationLandscapeRight:
            angle = startingRotation + -M_PI_2;
            break;
        default:
            angle = startingRotation;
            break;
    }

    angle;
}));

最後に、最初の方向から 90 度の方向に表示された黒いバーを削除しようとします。(これは iOS8 だけの問題かもしれません。) もう少し詳しく説明すると、縦向きUIImagePickerControllerモードにして横向きにすると、プレビューの上下に黒いバーが表示されます。これに対する解決策は、スケーリングではなく、スーパービューのクリッピングを削除することです。この試行は 1 回だけ行う必要があるため、最初にこのコードを呼び出したかどうかを確認します。また、回転した場合にのみこのコードを呼び出すようにしてください。最初のオリエンテーションで呼び出された場合、すぐには機能しません。

if (!self.didAttemptToRemoveCropping && self.startingOrientation != [UIApplication sharedApplication].statusBarOrientation) {
    self.didAttemptToRemoveCropping = YES;

    [self findClippedSubviewInView:self.view];
}

最後に、 for のコードで、-findClippedSubviewInView:すべてのサブビューをループして、 でビューを検索します.clipsToBounds = YES。それが正しい場合、祖先のスーパービューの 1 つが正しいことを確認するための条件をもう 1 つ作成します。

for (UIView* subview in view.subviews) {
    if (subview.clipsToBounds) {
        if ([self hasAncestorCameraView:subview]) {
            subview.clipsToBounds = NO;
            break;
        }
    }

    [self findClippedSubviewInView:subview];
}

では、-hasAncestorCameraView:単にスーパービュー チェーンをループして、クラスの 1 つがCameraView名前に含まれている場合は true を返します。

if (view == self.view) {
    return NO;
}

NSString* className = NSStringFromClass([view class]);

if ([className rangeOfString:@"CameraView"].location != NSNotFound) {
    return YES;

} else {
    return [self hasAncestorCameraView:view.superview];
}

これがコードの内訳です。ここにすべてまとめてあります。

#import <AVFoundation/AVFoundation.h>
#import "GImagePickerController.h"

@interface GImagePickerController ()
@property (nonatomic) UIInterfaceOrientation startingOrientation;
@property (nonatomic) BOOL didAttemptToRemoveCropping;
@end

@implementation GImagePickerController

- (instancetype)init {
    self = [super init];
    if (self) {

        if ([[[UIDevice currentDevice] systemVersion] intValue] >= 8) {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationDidChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(captureSessionDidStart:) name:AVCaptureSessionDidStartRunningNotification object:nil];
        }

    }
    return self;
}

- (void)dealloc {
    if ([[[UIDevice currentDevice] systemVersion] intValue] >= 8) {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
}


#pragma mark - Capture Session

- (void)captureSessionDidStart:(NSNotification *)notification {
    if (self.view.window && self.sourceType == UIImagePickerControllerSourceTypeCamera) {
        [self updateStartingOrientation];
    }
}


#pragma mark - Orientation

- (void)updateStartingOrientation {
    self.startingOrientation = [UIApplication sharedApplication].statusBarOrientation;
    [self updateCameraTransform];
}

- (void)updateCameraTransform {
    CGFloat startingRotation = ({
        CGFloat rotation;

        switch (self.startingOrientation) {
            case UIInterfaceOrientationPortraitUpsideDown:
                rotation = M_PI;
                break;
            case UIInterfaceOrientationLandscapeLeft:
                rotation = -M_PI_2;
                break;
            case UIInterfaceOrientationLandscapeRight:
                rotation = M_PI_2;
                break;
            default:
                rotation = 0.0f;
                break;
        }

        rotation;
    });

    self.cameraViewTransform = CGAffineTransformMakeRotation(({
        CGFloat angle;

        switch ([UIApplication sharedApplication].statusBarOrientation) {
            case UIInterfaceOrientationPortraitUpsideDown:
                angle = startingRotation + M_PI;
                break;
            case UIInterfaceOrientationLandscapeLeft:
                angle = startingRotation + M_PI_2;
                break;
            case UIInterfaceOrientationLandscapeRight:
                angle = startingRotation + -M_PI_2;
                break;
            default:
                angle = startingRotation;
                break;
        }

        angle;
    }));

    if (!self.didAttemptToRemoveCropping && self.startingOrientation != [UIApplication sharedApplication].statusBarOrientation) {
        self.didAttemptToRemoveCropping = YES;

        [self findClippedSubviewInView:self.view];
    }
}

- (void)statusBarOrientationDidChange:(NSNotification *)notification {
    if (self.view.window && self.sourceType == UIImagePickerControllerSourceTypeCamera) {
        [self updateCameraTransform];
    }
}


#pragma mark - Remove Clip To Bounds

- (BOOL)hasAncestorCameraView:(UIView *)view {
    if (view == self.view) {
        return NO;
    }

    NSString* className = NSStringFromClass([view class]);

    if ([className rangeOfString:@"CameraView"].location != NSNotFound) {
        return YES;

    } else {
        return [self hasAncestorCameraView:view.superview];
    }
}

- (void)findClippedSubviewInView:(UIView *)view {
    for (UIView* subview in view.subviews) {
        if (subview.clipsToBounds) {
            if ([self hasAncestorCameraView:subview]) {
                subview.clipsToBounds = NO;
                break;
            }
        }

        [self findClippedSubviewInView:subview];
    }
}

@end
于 2014-11-27T06:03:14.823 に答える
1

カメラ付き iPad - ポップオーバーに表示しないでください。代わりに、iPhone の場合と同様に、モーダル ビュー コントローラーで表示します。(少なくとも iOS 7 以降)

于 2014-05-04T21:47:10.050 に答える