gpt4 book ai didi

ios - 自定义相机需要很长时间才能保存图像(Swift 和 AVFoundation)

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

我下面的代码是一个可以工作的自定义相机。我的目标是在这里拍照并将其大小调整为正方形。之后,我将图像分配给 UIImage,然后通过 segue 传递到后 Controller 。转场最终发生在图像本身保存之前。我知道这一点是因为会发生转场,几秒钟后控制台将打印 UIImage 的值。

class CameraViewController: UIViewController {


@IBOutlet weak var photoButton: UIButton!
@IBOutlet weak var previewView: UIView!


var captureSession: AVCaptureSession?
var stillImageOutput: AVCaptureStillImageOutput?
var previewLayer: AVCaptureVideoPreviewLayer?
var buttonImage = UIImage(named: "CameraButton.png")
var captImage = UIImage()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

photoButton.setBackgroundImage(buttonImage, forState: .Normal)

captureSession = AVCaptureSession()
captureSession!.sessionPreset = AVCaptureSessionPresetPhoto

let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

var error: NSError?
var input: AVCaptureDeviceInput!
do {
input = try AVCaptureDeviceInput(device: backCamera)
} catch let error1 as NSError {
error = error1
input = nil
}

if error == nil && captureSession!.canAddInput(input) {
captureSession!.addInput(input)

stillImageOutput = AVCaptureStillImageOutput()
stillImageOutput!.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
if captureSession!.canAddOutput(stillImageOutput) {
captureSession!.addOutput(stillImageOutput)

previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation.Portrait
previewView.layer.addSublayer(previewLayer!)

captureSession!.startRunning()
}
}

}

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
previewLayer!.frame = previewView.bounds
}

@IBAction func didPressTakePhoto(sender: UIButton) {

if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) {
videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {(sampleBuffer, error) in
if (sampleBuffer != nil) {
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
let dataProvider = CGDataProviderCreateWithCFData(imageData)
let cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, CGColorRenderingIntent.RenderingIntentDefault)

var image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)
//Crop the image to a square
let imageSize: CGSize = image.size
let width: CGFloat = imageSize.width
let height: CGFloat = imageSize.height
if width != height {
let newDimension: CGFloat = min(width, height)
let widthOffset: CGFloat = (width - newDimension) / 2
let heightOffset: CGFloat = (height - newDimension) / 2
UIGraphicsBeginImageContextWithOptions(CGSizeMake(newDimension, newDimension), false, 0.0)
image.drawAtPoint(CGPointMake(-widthOffset, -heightOffset), blendMode: .Copy, alpha: 1.0)
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.captImage = image

print(self.captImage)
}

}
})
self.performSegueWithIdentifier("fromCustomCamera", sender: self)
}


}

@IBAction func didPressTakeAnother(sender: AnyObject) {
captureSession!.startRunning()
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "fromCustomCamera"{
let svc = segue.destinationViewController as! PostViewController
svc.imageRec = self.captImage
}
}
}

最佳答案

如果要确保调用完成后执行,则必须在异步函数的完成处理程序中调用 performSegueWithIdentifier

关于ios - 自定义相机需要很长时间才能保存图像(Swift 和 AVFoundation),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35308204/

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