gpt4 book ai didi

ios - 如何使用 AVFoundation 将预览(缩放)图像保存为照片?

转载 作者:行者123 更新时间:2023-11-28 06:32:49 24 4
gpt4 key购买 nike

所以,我已经弄清楚了如何使用 CATransform3DMakeScale(2.4, 2.4, 2.4) 制作缩放效果,但现在我在尝试保存 Zoome 预览消息时遇到了问题(在我进行缩放时编写代码):

 // init camera device
let captureDevice : AVCaptureDevice? = initCaptureDevice()
print("camera was initialized")
// Prepare output
initOutput()
if (captureDevice != nil) {
let deviceInput : AVCaptureInput? = initInputDevice(captureDevice: captureDevice!)
if (deviceInput != nil) {
initSession(deviceInput: deviceInput!)
// Preview Size
let previewLayer: AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: self.session)
previewLayer.frame = self.view.bounds
previewLayer.transform = CATransform3DMakeScale(2.4, 2.4, 2.4)
imagePreviewScale = previewLayer.contentsScale
self.view.layer.addSublayer(previewLayer)
self.session?.startRunning()

现在我尝试像这样保存预览的缩放图像:

 let videoConnection : AVCaptureConnection? = self.imageOutput?.connection(withMediaType: AVMediaTypeVideo)
if (videoConnection != nil) {

videoConnection?.videoScaleAndCropFactor = imagePreviewScale

self.imageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (imageDataSampleBuffer, error) -> Void in
if (imageDataSampleBuffer != nil) {
// Capture JPEG

let imageData : NSData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer) as NSData
// JPEG
let image = UIImage(data: imageData as Data)

并添加了行

 imagePreviewScale = previewLayer.contentsScale

但是还是没有任何反应,请问谁能告诉我如何保存精确缩放的图片?

最佳答案

问题是 previewLayer.contentsScale 为 1,因此您要将 videoScaleAndCropFactor 设置为 1。

你需要将它设置为

videoConnection?.videoScaleAndCropFactor = 2.4

关于ios - 如何使用 AVFoundation 将预览(缩放)图像保存为照片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39828518/

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