下面的代码是一个用作相机的UIView。按下按钮(didtakePhoto)时,在UIView周围插入边框。但是,边框未保存到照片中。该按钮会自动将照片保存到照片库,而不会在照片中插入边框。我想要的只是将sa.layer.addBorder
添加到已保存的照片中。
我的代码
import UIKit
import AVFoundation
class ViewController: UIViewController,AVCapturePhotoCaptureDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate {
var captureSesssion : AVCaptureSession!
var cameraOutput : AVCapturePhotoOutput!
var previewLayer : AVCaptureVideoPreviewLayer!
@IBOutlet var sa: UIView!
override func viewDidLoad() {
super.viewDidLoad()
captureSesssion = AVCaptureSession()
captureSesssion.sessionPreset = AVCaptureSessionPresetPhoto
cameraOutput = AVCapturePhotoOutput()
let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
if let input = try? AVCaptureDeviceInput(device: device) {
if (captureSesssion.canAddInput(input)) {
captureSesssion.addInput(input)
if (captureSesssion.canAddOutput(cameraOutput)) {
captureSesssion.addOutput(cameraOutput)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSesssion)
//previewLayer.frame = sa.bounds
previewLayer.frame = CGRect(x: 10, y: 10, width: 700, height: 700)
sa.layer.addSublayer(previewLayer)
captureSesssion.startRunning()
}}}}
@IBAction func didPressTakePhoto(_ sender: UIButton) {
sa.layer.addBorder(edge: [.top], color: UIColor.red, thickness: 12.0)
sa.layer.addBorder(edge: [.bottom], color: UIColor.red, thickness: 180.0)
sa.layer.addBorder(edge: [.left], color: UIColor.red, thickness: 100.0)
sa.layer.addBorder(edge: [.right], color: UIColor.red, thickness: 50.0)
let settings = AVCapturePhotoSettings()
let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first!
let previewFormat = [
kCVPixelBufferPixelFormatTypeKey as String: previewPixelType,
kCVPixelBufferWidthKey as String: 160,
kCVPixelBufferHeightKey as String: 160
]
settings.previewPhotoFormat = previewFormat
cameraOutput.capturePhoto(with: settings, delegate: self)
}
func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {
if let error = error {
print("error occure : \(error.localizedDescription)")
}
if let sampleBuffer = photoSampleBuffer,
let previewBuffer = previewPhotoSampleBuffer,
let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) {
print(UIImage(data: dataImage)?.size as Any)
let dataProvider = CGDataProvider(data: dataImage as CFData)
let cgImageRef: CGImage! = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
let image = UIImage(cgImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.right)
UIImageWriteToSavedPhotosAlbum(image, self, #selector(self.imagex(_:didFinishSavingWithError:contextInfo:)), nil)
} else {
print("some error here")
}}
func imagex(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
}}