2017-05-09 8 views
0

내 코드는 사진을 찍어 avfoundation을 사용하여 사진 갤러리에 저장합니다.AVfoundation (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) 
     } 
    } 

답변

0

단순히 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 
    } 
}