0
我的代碼使用avfoundation將照片存入照片庫。我只是不知道在哪裏可以要求一個被保存到相冊照片中也轉移到這是圖像視圖:如何使用AVfoundation將圖片放置在imageView上(swift3)
@IBOutlet var placeImage: UIImageView!.
代碼:
import UIKit
import AVFoundation
class ViewController: UIViewController, AVCapturePhotoCaptureDelegate {
@IBOutlet var cameraDisplay: UIView!
var captureSession : AVCaptureSession!
var cameraOutput : AVCapturePhotoOutput!
var previewLayer : AVCaptureVideoPreviewLayer!
@IBOutlet var placeImage: UIImageView!
var currentImageView: UIImageView?
override func viewDidLoad() {
super.viewDidLoad()
captureSession = AVCaptureSession()
captureSession.sessionPreset = AVCaptureSessionPresetPhoto
cameraOutput = AVCapturePhotoOutput()
let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
if let input = try? AVCaptureDeviceInput(device: device) {
if (captureSession.canAddInput(input)) {
captureSession.addInput(input)
if (captureSession.canAddOutput(cameraOutput)) {
captureSession.addOutput(cameraOutput)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.frame = CGRect(x: 10, y: 10, width: 300, height: 300)
// cameraDisplay.transform = cameraDisplay.transform.rotated(by: CGFloat(M_PI_2))
cameraDisplay.transform = cameraDisplay.transform.inverted()
cameraDisplay.layer.addSublayer(previewLayer)
captureSession.startRunning()
}
}
}
}
@IBAction func TakePhoto(_ sender: Any) {
let settings = AVCapturePhotoSettings()
let previewpixel = settings.availablePreviewPhotoPixelFormatTypes.first!
let previewformat = [
kCVPixelBufferPixelFormatTypeKey as String: previewpixel, kCVPixelBufferWidthKey as String: 160,
kCVPixelBufferHeightKey as String : 160]
settings.previewPhotoFormat = previewformat
cameraOutput.capturePhoto(with: settings, delegate: self)
}
func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
if let error = error {
let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
} else {
let ac = UIAlertController(title: "Image Saved!", message: "Your image has been saved to your photos.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
}
func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {
if let sampleBuffer = photoSampleBuffer,
let previewBuffer = previewPhotoSampleBuffer,
let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer){
let dataProvider = CGDataProvider(data: dataImage as CFData)
let cgImageRef: CGImage! = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
let imagea = UIImage(cgImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.down)
UIImageWriteToSavedPhotosAlbum(imagea, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)
}
}