gpt4 book ai didi

ios - captureStillImageAsynchronously 问题

转载 作者:行者123 更新时间:2023-11-30 11:48:27 25 4
gpt4 key购买 nike

我目前遇到 AVCaptureStillImageOutput 问题,当我尝试拍照时,图像当前为零。我当前尝试修复错误发现 captureStillImageAsynchronously 方法根本没有被调用,并且我无法测试示例缓冲区是否为零。我正在使用此方法将相机图像输入到另一种方法中,该方法将相机图像和另一个图像组合成单个图像。线程在最后一个方法期间失败。当我尝试通过捕获方法检查图像时,它不可用。我需要做什么才能让相机捕获正常工作?

public func capturePhotoOutput()->UIImage
{
var image:UIImage = UIImage()
if let videoConnection = stillImageOutput!.connection(withMediaType: AVMediaTypeVideo)
{
print("Video Connection established ---------------------")
stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: {(sampleBuffer, error) in
if (sampleBuffer != nil)
{
print("Sample Buffer not nil ---------------------")
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
let dataProvider = CGDataProvider(data: imageData! as CFData)
let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
let camImage = UIImage(cgImage: cgImageRef!, scale: CGFloat(1.0), orientation: UIImageOrientation.right)
image = camImage
}
else
{
print("nil sample buffer ---------------------")
}
})

}
if (stillImageOutput?.isCapturingStillImage)!
{
print("image capture in progress ---------------------")
}
else
{
print("capture not in progress -------------------")
}
return image
}

编辑:添加了以下使用相机图像的方法。

func takePicture()-> UIImage
{
/*
videoComponent!.getVideoController().capturePhotoOutput
{ (image) in
//Your code
guard let topImage = image else
{
print("No image")
return
}
}
*/
let topImage = videoComponent!.getVideoController().capturePhotoOutput() //overlay + Camera
let bottomImage = captureTextView() //text

let size = CGSize(width:(topImage.size.width),height:(topImage.size.height)+(bottomImage.size.height))

UIGraphicsBeginImageContextWithOptions(size, false, 0.0)

topImage.draw(in: CGRect(x:0, y:0, width:size.width, height: (topImage.size.height)))
bottomImage.draw(in: CGRect(x:(size.width-bottomImage.size.width)/2, y:(topImage.size.height), width: bottomImage.size.width, height: (bottomImage.size.height)))

let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}

最佳答案

如果您使用异步方法,该函数将返回错误值,因为异步调用仍在进行中。您可以使用完成 block ,如下所示:

public func capturePhotoOutput(completion: (UIImage?) -> ())
{
if let videoConnection = stillImageOutput!.connection(withMediaType: AVMediaTypeVideo)
{
print("Video Connection established ---------------------")
stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: {(sampleBuffer, error) in
if (sampleBuffer != nil)
{
print("Sample Buffer not nil ---------------------")
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
let dataProvider = CGDataProvider(data: imageData! as CFData)
let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
let camImage = UIImage(cgImage: cgImageRef!, scale: CGFloat(1.0), orientation: UIImageOrientation.right)
completion(camImage)
}
else
{
completion(nil)
}
})
}
else
{
completion(nil)
}
}

使用方法:

capturePhotoOutput
{ (image) in

guard let topImage = image else{
print("No image")
return
}

//Your code

}

编辑:

func takePicture()
{
videoComponent!.getVideoController().capturePhotoOutput
{ (image) in

guard let topImage = image else
{
print("No image")
return
}

let bottomImage = self.captureTextView() //text

let size = CGSize(width:(topImage.size.width),height:(topImage.size.height)+(bottomImage.size.height))

UIGraphicsBeginImageContextWithOptions(size, false, 0.0)

topImage.draw(in: CGRect(x:0, y:0, width:size.width, height: (topImage.size.height)))
bottomImage.draw(in: CGRect(x:(size.width-bottomImage.size.width)/2, y:(topImage.size.height), width: bottomImage.size.width, height: (bottomImage.size.height)))

let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()

self.setPicture(image: newImage)
}
}

func setPicture(image:UIImage)
{
//Your code after takePicture
}

关于ios - captureStillImageAsynchronously 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48568724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com