나는 captureStillImageAsynchronouslyFromConnection()
- 방법을 통해 카메라로 사진을 찍을 수있는 기능이 있습니다. 캡처 한 이미지는 capture()
- 메소드의 반환 값입니다.캡처 한 사진을 메인 스레드로 가져 오는 방법은 무엇입니까?
캡쳐 한 사진을 내 ViewController의 변수에 저장하고 다른 뷰 콘트롤러로 segue를 수행하여 사진을 표시하려고합니다. 이것은 작동하지 않는 것 같아 fatal error: unexpectedly found nil while unwrapping an Optional value
- 오류가 발생합니다.
나는 var photo: UIImage!
이 nil
인 것을 알고 있지만, performSegue...
함수를 호출하기 전에 어떻게 사진 데이터를 메인 스레드로 가져올 수 있습니까?
내 사진 기능 (내 내부 CameraSession 클래스)과 세션 큐 :
var sessionQueue = dispatch_queue_create("CameraSession", DISPATCH_QUEUE_SERIAL)
func capture(saveToGallery save: Bool) -> UIImage {
var capturedImage = UIImage()
let connection = self.output.connectionWithMediaType(AVMediaTypeVideo)
connection.videoOrientation = .Portrait
dispatch_async(sessionQueue, {
self.output.captureStillImageAsynchronouslyFromConnection(
connection, completionHandler: {
(imageDataSampleBuffer: CMSampleBuffer?, error: NSError?) -> Void in
if imageDataSampleBuffer != nil {
let imageData: NSData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
self.capturedImage = UIImage(data: imageData)
if save {
UIImageWriteToSavedPhotosAlbum(self.capturedImage!, nil, nil, nil)
}
}
}
)
})
return self.capturedImage!
}
의 ViewController 클래스에 내 기능 :
func capture() {
let photo = cameraSession.capture(saveToGallery: true)
performSegueWithIdentifier("showPhoto", sender: self) // This will switch to another ViewController, which shows the captured photo.
}
이미지가의 ViewController 클래스에서 촬영되지 않은, 그래서 나는를 추가 할 수 없습니다 그것에 segue. 'self.capturedImage'가 nil이 아니고 반환 할 때까지 기다리는 기능을 강요 할 수 있습니까? – user3653164
Gargoyle이 설명하는 것과 같은 것을 권하고, 블록을 캡처 메소드의 두 번째 인수로 전달합니다. 이 아이디어를 반영하여 내 대답을 업데이트했습니다. – J2K