gpt4 book ai didi

ios - 在 Swift 中使用 AVFoundation 拍摄照片

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

我正在快速构建一个实时滤镜相机应用程序。

我将从 AVCaptureVideoDataOutput() 更改为 AVCaptureStillImageOutput() 来执行捕获照片功能。更改后,打开应用程序时没有预览 View 。

抓拍照片功能正常,点击主组区域时可以听到“咔”的抓拍声。这里没有风景。

这是我的完整代码

import Foundation
import UIKit
import AVFoundation
import CoreMedia

let CIHueAdjust = "CIHueAdjust"
let CIHueAdjustFilter = CIFilter(name: "CIHueAdjust", withInputParameters: ["inputAngle" : 1.24])

let Filters = [CIHueAdjust: CIHueAdjustFilter]

let FilterNames = [String](Filters.keys).sort()

class LiveCamViewController : UIViewController,AVCaptureVideoDataOutputSampleBufferDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
let mainGroup = UIStackView()
let imageView = UIImageView(frame: CGRectZero)
let filtersControl = UISegmentedControl(items: FilterNames)
var videoOutput = AVCaptureStillImageOutput()

override func viewDidLoad()
{
super.viewDidLoad()

view.addSubview(mainGroup)
mainGroup.axis = UILayoutConstraintAxis.Vertical
mainGroup.distribution = UIStackViewDistribution.Fill

mainGroup.addArrangedSubview(imageView)
mainGroup.addArrangedSubview(filtersControl)
mainGroup.addGestureRecognizer(UITapGestureRecognizer(target: self, action:#selector(LiveCamViewController.saveToCamera(_:))))

imageView.contentMode = UIViewContentMode.ScaleAspectFit

filtersControl.selectedSegmentIndex = 0

let captureSession = AVCaptureSession()
captureSession.sessionPreset = AVCaptureSessionPresetPhoto

let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

do
{
let input = try AVCaptureDeviceInput(device: backCamera)

captureSession.addInput(input)
}
catch
{
print("can't access camera")
return
}

//get captureOutput invoked
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
view.layer.addSublayer(previewLayer)

videoOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]

if captureSession.canAddOutput(videoOutput)
{
captureSession.addOutput(videoOutput)
}

captureSession.startRunning()
}

func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!)
{
guard let filter = Filters[FilterNames[filtersControl.selectedSegmentIndex]] else
{
return
}

let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
let cameraImage = CIImage(CVPixelBuffer: pixelBuffer!)

filter!.setValue(cameraImage, forKey: kCIInputImageKey)

let filteredImage = UIImage(CIImage: filter!.valueForKey(kCIOutputImageKey) as! CIImage!)
let fixedImage = correctlyOrientedImage(filteredImage)

dispatch_async(dispatch_get_main_queue())
{
self.imageView.image = fixedImage
}

}

func correctlyOrientedImage(image: UIImage) -> UIImage {

UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale)
image.drawInRect(CGRectMake(0, 0, image.size.width, image.size.height))
let normalizedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

let imageRef: CGImageRef = normalizedImage.CGImage!
let rotatedImage: UIImage = UIImage(CGImage: imageRef, scale: 1.0, orientation: .Right)

return rotatedImage
}

override func viewDidLayoutSubviews()
{
mainGroup.frame = CGRect(x: 37, y: 115, width: 301, height: 481)
}

func saveToCamera(sender: UITapGestureRecognizer) {

videoOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]

if let videoConnection = videoOutput.connectionWithMediaType(AVMediaTypeVideo) {
videoOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
(imageDataSampleBuffer, error) -> Void in
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
UIImageWriteToSavedPhotosAlbum(UIImage(data: imageData)!, nil, nil, nil)
}
}
}

}

谢谢。

最佳答案

如果您想从相机捕获照片或视频,那么您应该使用 UIImagePickerController 而不是 AVFoundation。检查Applce documentation了解更多相关信息。

更新:

引用this link 。它有太多自定义imagePickercontroller的例子。您可以引用this this stackobverflow 的答案。

希望这会有所帮助:)

关于ios - 在 Swift 中使用 AVFoundation 拍摄照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36963565/

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