“CIImage initWithCVPixelBuffer:options:”失败,因为iOS 10中不支持其像素格式p422

时间:2016-09-21 08:02:57

标签: swift avfoundation avplayer ciimage avplayeritem

在ios10中,我希望获得视频捕获,但是出现错误“[CIImage initWithCVPixelBuffer:options:]失败,因为它不支持像素格式p422。”

我的代码是:

func previewImage(handle: (image: UIImage?) -> Void) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                guard let time = self.player?.currentTime() else {
                    return
                }
                guard let pixelBuffer = self.output?.copyPixelBufferForItemTime(time,
                    itemTimeForDisplay: nil) else {
                        return
                }
                let ciImage = CIImage(CVPixelBuffer: pixelBuffer)
                let temporaryContext = CIContext(options: nil)
                let rect = CGRectMake(
                    0,
                    0,
                    CGFloat(CVPixelBufferGetWidth(pixelBuffer)),
                    CGFloat(CVPixelBufferGetHeight(pixelBuffer))
                )

                let image: UIImage?
                if let videoImage = temporaryContext.createCGImage(ciImage, fromRect: rect) {
                    image = UIImage(CGImage: videoImage)
                }
                else {
                    image = nil
                }
                handle(image: image)
            })
        })

    }

收到如下错误:

enter image description here

我该怎么办?

1 个答案:

答案 0 :(得分:3)

你能说明你是如何设置kCVPixelFormatType_420YpCbCr8BiPlanarFullRange的吗?

看起来你正在使用一些(我不知道,p422?)4:2:2格式,而不是像kCVPixelFormatType_32BGRAdeclare @sql nvarchar(max) set @sql = N'alter table pc$ drop column f' + cast(@colnum as nvarchar(16)) exec (@sql) 这样更兼容的格式。