ios - 无视频输出和 [MC] 从公共(public)有效用户设置中读取。 Swift/iOS 11 错误

标签 ios swift avfoundation ios11 xcode9-beta

使用适用于 iOS 11 的 Xcode 9 Beta:

我关注了 walkthrough关于如何从 AVCaptureSession 中提取帧,但无法使捕获出现。虽然我在 info.plist 文件中包含了相机权限,但该应用程序似乎在打开后停止运行,并且出现以下错误:

[App Name] does not have sandbox access for frZQaeyWLUvLjeuEK43hmg and IS NOT appropriately entitled

[MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles

[MC] Reading from public effective user settings.

下面是FrameExtractor.swift的代码供引用:

import UIKit
import AVFoundation

protocol FrameExtractorDelegate: class {
    func captured(image: UIImage)
}

class FrameExtractor: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {

private let position = AVCaptureDevice.Position.front
private let quality = AVCaptureSession.Preset.medium

private var permissionGranted = false
private let sessionQueue = DispatchQueue(label: "session queue")
private let captureSession = AVCaptureSession()
private let context = CIContext()

weak var delegate: FrameExtractorDelegate?

override init() {
    super.init()
    checkPermission()
    sessionQueue.async { [unowned self] in
        self.configureSession()
        self.captureSession.startRunning()
    }
}

// MARK: AVSession configuration
private func checkPermission() {
    switch AVCaptureDevice.authorizationStatus(for: AVMediaType.video) {
    case .authorized:
        permissionGranted = true
    case .notDetermined:
        requestPermission()
    default:
        permissionGranted = false
    }
}

private func requestPermission() {
    sessionQueue.suspend()
    AVCaptureDevice.requestAccess(for: AVMediaType.video) { [unowned self] granted in
        self.permissionGranted = granted
        self.sessionQueue.resume()
    }
}

private func configureSession() {
    guard permissionGranted else { return }
    captureSession.sessionPreset = quality
    guard let captureDevice = selectCaptureDevice() else { return }
    guard let captureDeviceInput = try? AVCaptureDeviceInput(device: captureDevice) else { return }
    guard captureSession.canAddInput(captureDeviceInput) else { return }
    captureSession.addInput(captureDeviceInput)
    let videoOutput = AVCaptureVideoDataOutput()
    videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "sample buffer"))
    guard captureSession.canAddOutput(videoOutput) else { return }
    captureSession.addOutput(videoOutput)
    guard let connection = videoOutput.connection(with: AVFoundation.AVMediaType.video) else { return }
    guard connection.isVideoOrientationSupported else { return }
    guard connection.isVideoMirroringSupported else { return }
    connection.videoOrientation = .portrait
    connection.isVideoMirrored = position == .front
}

private func selectCaptureDevice() -> AVCaptureDevice? {
    return AVCaptureDevice.default(for: AVMediaType.video)
//        return AVCaptureDevice.devices().filter {
//            ($0 as AnyObject).hasMediaType(AVMediaType.video) &&
//                ($0 as AnyObject).position == position
//            }.first
}

// MARK: Sample buffer to UIImage conversion
private func imageFromSampleBuffer(sampleBuffer: CMSampleBuffer) -> UIImage? {
    guard let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return nil }
    let ciImage = CIImage(cvPixelBuffer: imageBuffer)
    guard let cgImage = context.createCGImage(ciImage, from: ciImage.extent) else { return nil }
    return UIImage(cgImage: cgImage)
}

// MARK: AVCaptureVideoDataOutputSampleBufferDelegate
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
    print("Got a Frame!")
    guard let uiImage = imageFromSampleBuffer(sampleBuffer: sampleBuffer) else { return }
    DispatchQueue.main.async { [unowned self] in
        self.delegate?.captured(image: uiImage)
    }
}
}

对于 ViewController.swift:

import UIKit

class ViewController: UIViewController, FrameExtractorDelegate{
@IBOutlet var imageView: UIImageView!
var frameExtractor: FrameExtractor!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    frameExtractor = FrameExtractor()
    frameExtractor.delegate = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func captured(image: UIImage) {
    imageView.image = image
}

}`

最佳答案

问题出在 captureOutput 中的不同函数调用中。这是 iOS 11 中对 AVCaptureVideoDataOutputSampleBufferDelegate 中的 captureOutput 的新函数调用:

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
    guard let uiImage = imageFromSampleBuffer(sampleBuffer: sampleBuffer) else { return }
    DispatchQueue.main.async { [unowned self] in
        self.delegate?.captured(image: uiImage)
    }
}

注意“didOutput sampleBuffer:”和“didOutputSampleBuffer sampleBuffer:”之间的变化

关于ios - 无视频输出和 [MC] 从公共(public)有效用户设置中读取。 Swift/iOS 11 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44447995/

相关文章:

ios - 如何将水平滚动添加到 UIView?

ios - 我可以在 ios8 中使用 libicucore.dylib,这是一个私有(private)图书馆吗?

arrays - 如何按升序排列一个字典数组,然后对其他数组重新排序?

ios - 使用 AVFoundation 类播放音频文件的引用部分

ios - AVAsset 维度 : track's naturalSize is not the size off the exported video

swift - 使用 replaceCurrentItem() 时 AVPlayer 闪烁 - Swift - 以编程方式

ios - 通过 UITableVIew 中的部分快速通用基本搜索功能

swift - 截断标签中的文本

ios - 使用 Core Location 在 Apple Watch 上获得精确的室内位置更新?

iphone - 如何通过 Xcode 在我的设备上运行发布配置