AVCaptureStillImageOutput区域select

在用Swift编写的iOS应用程序中保存从相机拍摄的图像时,这是我面临的挑战。 我并不是完全按照自己的意愿去储蓄,而是在储蓄之外,而不是那么做。

这是这个问题的两个相关的代码块。

首先在会议开始的地方,人们可以看到我只看着一个椭圆形区域:

previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) previewLayer?.frame = self.view.layer.frame self.view.layer.addSublayer(previewLayer!) let maskLayer = CAShapeLayer() maskLayer.path = CGPathCreateWithEllipseInRect(CGRect(x: 50.0, y: 100.0, width: 200.0, height: 100.0), nil) previewLayer!.mask = maskLayer captureSession.startRunning() 

第二,保存图像并停止会话:

  if captureSession.running { if let videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo) { stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) { (imageDataSampleBuffer, error) -> Void in if error == nil { let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer) UIImageWriteToSavedPhotosAlbum(UIImage(data: imageData)!, nil, nil, nil) } else {print("Error on taking a picture:\n\(error)")} } } captureSession.stopRunning() } 

这是行得通的,但是当我在相册中查看已经保存的内容时,我发现整个图片看起来好像没有限制椭圆,这不是我想要的。 理想情况下,我应该只保存在椭圆内的像素。 如果这是不可能的,我应该至less保存在椭圆内的像素和椭圆外的像素作为一个唯一的透明颜色。 有没有办法来控制AVCaptureStillImageOutput如何工作,或者我需要做什么来实现我想要的?