如何从AVCapturePhotoOutput获取图像网址?

时间:2019-11-20 06:29:26

标签: swift

当我单击按钮时,它使用AVCapturePhotoOutput捕获照片并将URL存储在数据库中,但是当我捕获照片时,它变黑。我上传了图片,您只能看一次。

enter image description here

let stillImageOutput = AVCapturePhotoOutput()
let settings = AVCapturePhotoSettings()


 @IBAction func capturephoto(_ sender: Any) {
    print("entered into capture photo")

    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

    captureSession.addOutput(stillImageOutput)
    print("stillimageoutput is",stillImageOutput)
    self.stillImageOutput.capturePhoto(with: settings, delegate: self)




}
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {


        if let error = error {
            print(error.localizedDescription)
        }

        if let sampleBuffer = photoSampleBuffer, let previewBuffer = previewPhotoSampleBuffer, let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) {

            print("image: \(UIImage(data: dataImage)?.size)") // Your Image
            //Here I should conver the dataImage into URL....

        }
    }

1 个答案:

答案 0 :(得分:0)

func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {
    if let error = error {
        print(error.localizedDescription)
    }

    if let sampleBuffer = photoSampleBuffer, let previewBuffer = previewPhotoSampleBuffer, let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) {

        let image: UIImage = UIImage(data:imageData,scale:1.0)
    }
}