简体   繁体   English

如何使用AVfoundation(swift3)将照片放置在imageView上

[英]how to place photo on imageView using AVfoundation (swift3)

My code takes a photo and saves it into photo gallery using avfoundation. 我的代码拍摄了一张照片,并使用avfoundation将其保存到照片库中。 I just don't know where to request that the photo that is being saved to photo gallery be also transferred to the image view which is: 我只是不知道在哪里要求将保存到照片库中的照片也转移到图像视图中:

@IBOutlet var placeImage: UIImageView!.

Code: 码:

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)
        }
    }

Simply use your didFinishSavingWithError method: 只需使用didFinishSavingWithError方法:

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 {
        placeImage.image = image
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM