如何使用 AVFoundation 将预览(缩放)图像保存为照片?
How to save the previewed (Zoomed) image as Photo using AVFoundation?
所以,我已经弄明白了如何使用 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
所以,我已经弄明白了如何使用 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