【问题标题】:How do I use the AVFoundation framework to take a picture with the iPhone camera? [duplicate]如何使用 AVFoundation 框架用 iPhone 相机拍照? [复制]
【发布时间】:2014-03-15 20:34:59
【问题描述】:

我需要 iOS 相机在没有用户输入的情况下拍照。我该怎么做呢?到目前为止,这是我的代码:

-(void)initCapture{
    AVCaptureDeviceInput *newVideoInput = [[AVCaptureDeviceInput alloc] init];

    AVCaptureStillImageOutput *newStillImageOutput = [[AVCaptureStillImageOutput alloc] init];

    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:
                                AVVideoCodecJPEG, AVVideoCodecKey,
                                nil];


    [newStillImageOutput setOutputSettings:outputSettings];


    AVCaptureSession *newCaptureSession = [[AVCaptureSession alloc] init];

    if ([newCaptureSession canAddInput:newVideoInput]) {
        [newCaptureSession addInput:newVideoInput];
    }
    if ([newCaptureSession canAddOutput:newStillImageOutput]) {
        [newCaptureSession addOutput:newStillImageOutput];
    }
    self.stillImageOutput = newStillImageOutput;
}

我还需要添加什么,我该从哪里开始?我不想拍视频,只拍一张静止图像。另外,之后我如何将图像转换为 UIImage ?谢谢

【问题讨论】:

    标签: ios objective-c avfoundation avcapturesession ios-camera


    【解决方案1】:

    根据this tutorial,您应该可以这样做:

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

    还有你的 UIImage 对象!

    【讨论】:

    • 是否有任何第三方框架可以从设备拍照。
    • 这是一个单独的问题@karthikeyan;在新问题中提出(并在新问题中解释为什么要避免使用 AVFoundation 框架)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 2018-06-19
    • 2021-08-09
    相关资源
    最近更新 更多