ios - 从 ViewController 取图片

标签 ios objective-c ipad

我正在制作一个带有 ScrollView 的应用程序。在一个屏幕上,我有很多 UIPopover。当您将页面滚动到新屏幕时,我希望能够在按下按钮后立即拍照。 目前我有一个启动相机的按钮和一个添加为子层的预览层

Current situation

代码:

- (IBAction)startCameraButtonPushed:(id)sender
{
    if (session.isRunning) {
        [startCamera setTitle:@"Start Camera" forState:UIControlStateNormal];
        previewLayer.hidden = YES;
        [session stopRunning];
        session = nil;
        previewLayer = nil;
    } else {
        [startCamera setTitle:@"Stop Camera" forState:UIControlStateNormal];
        session = [[AVCaptureSession alloc] init];
        AVCaptureOutput *output = [[AVCaptureStillImageOutput alloc] init];
        [session addOutput:output];
        NSArray *possibleDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
        AVCaptureDevice *device = [possibleDevices objectAtIndex:0];
        NSError *error = nil;
        AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
        session.sessionPreset = AVCaptureSessionPresetPhoto;

        [session addInput:input];

        previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];

        previewLayer.frame = CGRectMake(3202, 100, 800, 400);
        previewLayer.hidden = YES;
        // previewLayer.delegate = self;
        [self.scrollView.layer addSublayer:previewLayer];
        previewLayer.hidden = NO;
        [session startRunning];
    }

}

如您所见,相机处于人像模式,我非常努力地让它变成风景模式。到目前为止没有运气。还很难找到一个按钮来实际拍照。 如果有人能帮我一点忙,我将不胜感激!

最佳答案

您可以转换预览层以使视频以正确的方向显示:

if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
    previewLayer.affineTransform = CGAffineTransformMakeRotation(0);
} else if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
    previewLayer.affineTransform = CGAffineTransformMakeRotation(M_PI/2);
} else if (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    previewLayer.affineTransform = CGAffineTransformMakeRotation(-M_PI/2);
} else if (self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    previewLayer.affineTransform = CGAffineTransformMakeRotation(M_PI);
}

关于ios - 从 ViewController 取图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24269566/

相关文章:

ios - XML 到对象 Objective-C

objective-c - 从数组中获取随机不重复的值

objective-c - 使用箭头键选择行时,NSTableView 不发送 "selected row"操作

ios - 使用缓冲从 objective-c 中的 url 播放视频

iphone - 创建电子书阅读应用程序的教程 - epub 文件格式

ios - mvvmcross showviewmodel byte[] 作为参数

android - 'Chrome Apps for Mobile' 是否在每个平台上使用 native WebView ?

ios - 如何从 NSDictionary 获取键/值对?

iphone - 按键拍照,无摄像头显示

iphone - 核心数据和半永久数据