2013-05-13 9 views
9

पर कैमरा पूर्वावलोकन प्राप्त करें मैं पूर्वावलोकन परत दृश्य पर दिखाने के लिए कैमरा इनपुट प्राप्त करने का प्रयास कर रहा था।AVCaptureVideoPreviewLayer

self.cameraPreviewView आईबी

में एक UIView से जुड़ा हुआ है यहाँ मेरे वर्तमान कोड है कि मैं ए वी फाउंडेशन प्रोग्रामिंग गाइड से एक साथ रखा है। लेकिन पूर्वावलोकन कभी नहीं चलता

AVCaptureSession *session = [[AVCaptureSession alloc] init]; 
    session.sessionPreset = AVCaptureSessionPresetHigh; 

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 

    NSError *error = nil; 
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; 

    if (!input) { 
     NSLog(@"Couldn't create video capture device"); 
    } 
    [session addInput:input]; 


     // Create video preview layer and add it to the UI 
     AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; 
     UIView *view = self.cameraPreviewView; 
     CALayer *viewLayer = [view layer]; 

     newCaptureVideoPreviewLayer.frame = view.bounds; 

     [viewLayer addSublayer:newCaptureVideoPreviewLayer]; 

     self.cameraPreviewLayer = newCaptureVideoPreviewLayer; 



     [session startRunning]; 
+0

आप नीचे दिए गए कोड की कोशिश कर सकते उल्लेख कर सकते हैं ... newCaptureVideoPreviewLayer.frame = self.cameraPreviewView.bounds; [self.cameraPreviewView.layer addSublayer: newCaptureVideoPreviewLayer]; [सत्र startRunning]; – BhushanVU

+0

@ बुहक्सन - बस कोशिश की और अभी भी काले दृश्य। –

उत्तर

19

तो कुछ परीक्षण और त्रुटि और सेब के AVCam नमूना कोड

देखने के बाद मैं एक भव्य केंद्रीय प्रेषण ब्लॉक में PreviewLayer कोड और सत्र startRunning की तरह इतना लपेटा जाता है और यह काम करना शुरू किया।

dispatch_async(dispatch_get_main_queue(), ^{ 
    AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; 
    UIView *view = self.cameraPreviewView; 
    CALayer *viewLayer = [view layer]; 

    newCaptureVideoPreviewLayer.frame = view.bounds; 

    [viewLayer addSublayer:newCaptureVideoPreviewLayer]; 

    self.cameraPreviewLayer = newCaptureVideoPreviewLayer; 

    [session startRunning]; 
}); 
+6

यहां सबसे मुश्किल पंक्ति ** [सत्र startRunning] है; ** क्योंकि यह सबसे अधिक ट्यूटोरियल में प्रस्तुत नहीं किया गया है, दुर्भाग्य से। –

+0

क्षमा करें मुझे समझ में नहीं आया कि 'self.cameraPreviewLayer' वास्तव में क्या है? उलझन में cuz यह स्पष्ट रूप से हमें एक त्रुटि देता है अगर यह एक uiview –

17

यहाँ, मेरे कोड, यह मेरे लिए एकदम सही काम करता है आप के लिए यह

- (void)initCapture 
{ 
    AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
    AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:nil]; 
    if (!captureInput) { 
     return; 
    } 
    AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init]; 
    /* captureOutput:didOutputSampleBuffer:fromConnection delegate method !*/ 
    [captureOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()]; 
    NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey; 
    NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA]; 
    NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key]; 
    [captureOutput setVideoSettings:videoSettings]; 
    self.captureSession = [[AVCaptureSession alloc] init]; 
    NSString* preset = 0; 
    if (!preset) { 
     preset = AVCaptureSessionPresetMedium; 
    } 
    self.captureSession.sessionPreset = preset; 
    if ([self.captureSession canAddInput:captureInput]) { 
     [self.captureSession addInput:captureInput]; 
    } 
    if ([self.captureSession canAddOutput:captureOutput]) { 
     [self.captureSession addOutput:captureOutput]; 
    } 

    //handle prevLayer 
    if (!self.captureVideoPreviewLayer) { 
     self.captureVideoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession]; 
    } 

    //if you want to adjust the previewlayer frame, here! 
    self.captureVideoPreviewLayer.frame = self.view.bounds; 
    self.captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
    [self.view.layer addSublayer: self.captureVideoPreviewLayer]; 
    [self.captureSession startRunning]; 
} 
+1

बेशक, आप इसे dispatch_async ब्लॉक से भी लपेट सकते हैं। – jianpx

+0

धन्यवाद आकर्षण की तरह काम किया :-) –

+0

क्या यह वास्तव में रिकॉर्ड होगा? – user717452

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