0

写真を撮り、写真をアルバムに保存するために AVCaptureSession を使用しています。ボタンをクリックすると、スナップショットが取得され、アルバムに保存されます。しかし、ランドスケープモードを使用すると、ボタンをクリックするとランドスケープモードの画像が保存されます。画像をプレビューすると、ポートレート モードになります。

コード:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self setCaptureSession:[[AVCaptureSession alloc] init]];

    [self  addVideoInputFrontCamera:NO]; // set to YES for Front Camera, No for Back camera

    [self  addStillImageOutput];

    [self setPreviewLayer:[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]] ];

    //  UIView *newView = [[UIView alloc] initWithFrame:self.view.bounds];
    // [newView.layer addSublayer: self.previewLayer];
    // [self.view addSubview: newView];

    //previewLayer.frame=self.previewLayer.bounds;

    [[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];

    //  [[self previewLayer] setVideoGravity:AVLayerVideoGravityResize];


  //  self.previewLayer.orientation = AVCaptureVideoOrientationPortrait;

    CGRect layerRect = [[[self view] layer] bounds];

    [[self previewLayer]setBounds:layerRect];
    [[self  previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),CGRectGetMidY(layerRect))];
    [[[self view] layer] addSublayer:[self  previewLayer]];

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveImageToPhotoAlbum) name:kImageCapturedSuccessfully object:nil];

    [[self captureSession] startRunning];

    camera=[UIButton buttonWithType:UIButtonTypeCustom];
    [camera setImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
    [camera setFrame:CGRectMake(150, 10, 40, 30)];
    [camera addTarget:self action:@selector(takephoto:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:camera];
}

スナップショットをキャプチャ:

- (void)takephoto:(id)sender
{
    [self captureStillImage];
}

- (void)captureStillImage
{
    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
        for (AVCaptureInputPort *port in [connection inputPorts]) {
            if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection) {
            break;
        }
    }

    NSLog(@"about to request a capture from: %@", [self stillImageOutput]);

    [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
                                                         completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
                                                             CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
                                                             if (exifAttachments) {
                                                                 NSLog(@"attachements: %@", exifAttachments);
                                                             } else {
                                                                 NSLog(@"no attachments");
                                                             }

                                                             NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
                                                             UIImage *image = [[UIImage alloc] initWithData:imageData];


                                                            [self setStillImage:image];



                                                             //  [image release];
                                                             [[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];

                                                         }];
}

self.previewLayer.orientation = UIInterfaceOrientationLandscapeLeft;ランドスケープモードで使用しています

4

1 に答える 1

1

この問題を修正するには、previewLayer の方向に加えて、videoOrientation を設定する必要があります。

AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
    for (AVCaptureInputPort *port in [connection inputPorts]) {
        if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
            videoConnection = connection;
            break;
        }
    }
    if (videoConnection) {
        break;
    }
}

この後

[videoConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];

天気アプリを横向きまたは縦向きモードでチェックして向きを設定します。

于 2014-05-16T19:47:08.947 に答える