2017-09-28 31 views
6

मैं अपने अनुप्रयोग में एक कस्टम कैमरा है और यह ठीक काम किया, लेकिन नया अद्यतन के बाद मैं इस त्रुटि हो रही है:लेते हुए फोटो

'jpegPhotoDataRepresentation(forJPEGSampleBuffer:previewPhotoSampleBuffer:)' was deprecated in iOS 11.0: Use -[AVCapturePhoto fileDataRepresentation] instead.

इस लाइन है

guard let imageData = 
     AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else { 
      return 
    } 

यह मेरे पूरे समारोह है (यदि आवश्यक):

//Take pic function 
func photoOutput(_ captureOutput: AVCapturePhotoOutput, 
       didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, 
       previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?, 
       resolvedSettings: AVCaptureResolvedPhotoSettings, 
       bracketSettings: AVCaptureBracketedStillImageSettings?, 
       error: Error?) { 


    // Make sure we get some photo sample buffer 
    guard error == nil, 
     let photoSampleBuffer = photoSampleBuffer else { 
      print("Error capturing photo: \(String(describing: error))") 
      return 
    } 
    // Convert photo same buffer to a jpeg image data by using // AVCapturePhotoOutput 
    guard let imageData = 
     AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else { 
      return 
    } 

    let dataProvider = CGDataProvider(data: imageData as CFData) 

    let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.absoluteColorimetric) 


    let image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.right) 

    self.tempImageView.image = image 

} 

मेरे guesti जहां मुझे लगता है कि त्रुटि मिल रही है चालू है: इसे काम करने के बजाय मुझे इसका क्या उपयोग करना चाहिए?

धन्यवाद।

उत्तर

11

आईओएस 11 में, आप इस तरह उपयोग करना चाहिए:

@available(iOS 11.0, *) 
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) { 
     let imageData = photo.fileDataRepresentation() 
} 
4

धन्यवाद @Vini अनुप्रयोग, मैं कोड यह मेरे लिए काम किया की कोशिश की, मैं छवि कैप्चरिंग और प्रसंस्करण, आशा लोगों में मदद मिलेगी के लिए मेरे कोड तैनात जिन्हें अपने ऐप में समान कार्य की आवश्यकता होती है।

सबसे पहले आप सेटअप की जरूरत है अपने वीडियो डिवाइस पर कब्जा करने, खोज इसे यहाँ गूगल एक उदाहरण https://gist.github.com/tad-iizuka/fc35bc7835920c0b8b84e316f83e3a40

सुनिश्चित करें कि आप शीर्ष

... 
var photoSetting = AVCapturePhotoSettings() 
... 

पर photoSetting परिभाषित करने के लिए तस्वीर या तो सेटिंग कॉन्फ़िगर की जरूरत है बनाता है viewDidLoad() या viewWillAppear()

  // Configure camera 
     photoSetting = AVCapturePhotoSettings.init(format: [AVVideoCodecKey: AVVideoCodecType.jpeg]) 
     photoSetting.isAutoStillImageStabilizationEnabled = true 
     photoSetting.flashMode = .off 

फिर पी के लिए निम्न फ़ंक्शन का उपयोग करें rocess बफ़र छवि डेटा

 func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) { 

    // Check if there is any error in capturing 
    guard error == nil else { 
     print("Fail to capture photo: \(String(describing: error))") 
     return 
    } 

    // Check if the pixel buffer could be converted to image data 
    guard let imageData = photo.fileDataRepresentation() else { 
     print("Fail to convert pixel buffer") 
     return 
    } 

    // Check if UIImage could be initialized with image data 
    guard let capturedImage = UIImage.init(data: imageData , scale: 1.0) else { 
     print("Fail to convert image data to UIImage") 
     return 
    } 

    // Get original image width/height 
    let imgWidth = capturedImage.size.width 
    let imgHeight = capturedImage.size.height 
    // Get origin of cropped image 
    let imgOrigin = CGPoint(x: (imgWidth - imgHeight)/2, y: (imgHeight - imgHeight)/2) 
    // Get size of cropped iamge 
    let imgSize = CGSize(width: imgHeight, height: imgHeight) 

    // Check if image could be cropped successfully 
    guard let imageRef = capturedImage.cgImage?.cropping(to: CGRect(origin: imgOrigin, size: imgSize)) else { 
     print("Fail to crop image") 
     return 
    } 

    // Convert cropped image ref to UIImage 
    imageToSave = UIImage(cgImage: imageRef, scale: 1.0, orientation: .down) 
    UIImageWriteToSavedPhotosAlbum(imageToSave, nil, nil, nil) 

    // Stop video capturing session (Freeze preview) 
    captureSession.stopRunning() 
} 

इस समारोह में, पिक्सेल बफर प्रारूप photoSetting द्वारा निर्दिष्ट में छवि डेटा में बदल जाती है और फिर आकार आप चाहते हैं के लिए क्रॉप।

आप टॉप अप ऊपर

@IBAction func onTakePhoto(_ sender: UIButton) { 
    if let videoConnection = videoOutput.connection(with: AVMediaType.video) { 
     // Adjust the orientaion of captured image 
     let capturePhotoSetting = AVCapturePhotoSettings.init(from: photoSetting) 
     videoConnection.videoOrientation = (previewLayer.connection?.videoOrientation)! 
     // Save captured photo to system album 
     self.videoOutput.capturePhoto(with: capturePhotoSetting, delegate: self) 
    } 
} 
+0

डिफाइनिंग photoSetting कैप्चरिंग समारोह कॉल करने के लिए एक चर के रूप में मेरे लिए काम नहीं किया, क्योंकि दूसरी तस्वीर से आईबी में एक बटन बना सकते हैं लिया गया था, आवेदन मैं कह दुर्घटनाग्रस्त फोटो सेटिंग्स का उपयोग करने की अनुमति नहीं थी। AVCapturePhotoSettings को आपके आईबीएक्शन में "चलो" (var नहीं) के साथ परिभाषित किया जा सकता है या हर बार जब आप फोटो लेते हैं, तो यह बार-बार फोटो लेने के साथ ठीक काम करेगा। – VagueExplanation

+1

हाय, @VagueExplanation, उत्तर के लिए धन्यवाद लेकिन मुझे समझ में नहीं आया कि वास्तव में दुर्घटना का कारण क्या है (हर बार जब मैं एवी कैप्चर फोटोोटोटिंग को तुरंत चालू करता हूं) लगता है। मेरे ऐप में 'फोटो बूथ' व्यू में पोस्ट किया गया कोड, मूल रूप से, आप एक फोटो ले सकते हैं और अगली बार देख सकते हैं कि आप संतुष्ट हैं या इसे फिर से लेते हैं यदि आपको यह पसंद नहीं है। तस्वीर एल्बम में सहेजी गई है और ऐप आईपैड पर चलता है। उम्मीद है कि मददगार होगा? या शायद आप चर्चा के लिए अधिक जानकारी प्रदान कर सकते हैं? –

+0

आह ठीक है, तो कभी बुरा मत मानो। यह इस बात पर निर्भर करता है कि आप इसका उपयोग कैसे करते हैं। – VagueExplanation

संबंधित मुद्दे