2012-09-13 3 views
1

아이폰에서 사용자가 알지 못하는 이미지를 캡처하고 싶습니다. 이를 위해 사용자 정의 오버레이를 사용하고 [imagePicker takePicture]라고 불렀지 만 didFinishPickingMediaWithInfo 메서드를 호출하지 않았습니다. 누구든지이 문제를 해결하도록 도와 줄 수 있습니까 ??아이폰에서 사용자의 개입없이 이미지 캡쳐

코드 :

imgPicker = [[UIImagePickerController alloc] init]; 
imgPicker.delegate = self; 
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera; 
imgPicker.cameraDevice=UIImagePickerControllerCameraDeviceFront; 
imgPicker.allowsEditing = NO; 
imgPicker.showsCameraControls = NO; 
imgPicker.wantsFullScreenLayout = NO; 
imgPicker.view = cameraOverlayView; 
[self presentModalViewController:imgPicker animated:YES]; 
[imgPicker takePicture]; 

[imgPicker dismissModalViewControllerAnimated:YES]; 

내가 그것을 할 도와주세요.

답변

2
@try 
    { 

     AVCaptureDevice *frontalCamera; 
     NSArray *allCameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; 

     // Find the frontal camera. 
     for (int i = 0; i < allCameras.count; i++) { 
      AVCaptureDevice *camera = [allCameras objectAtIndex:i]; 

      if (camera.position == AVCaptureDevicePositionFront) { 
       frontalCamera = camera; 
      } 
     } 

     // If we did not find the camera then do not take picture. 
     if (frontalCamera != nil) { 
      // Start the process of getting a picture. 
      session = [[AVCaptureSession alloc] init]; 

      // Setup instance of input with frontal camera and add to session. 
      NSError *error; 
      AVCaptureDeviceInput *input = 
      [AVCaptureDeviceInput deviceInputWithDevice:frontalCamera error:&error]; 

      if (!error && [session canAddInput:input]) { 
       // Add frontal camera to this session. 
       [session addInput:input]; 

       // We need to capture still image. 
       AVCaptureStillImageOutput *output = [[AVCaptureStillImageOutput alloc] init]; 

       // Captured image. settings. 
       [output setOutputSettings: 
       [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil]]; 

       if ([session canAddOutput:output]) { 
        [session addOutput:output]; 

        AVCaptureConnection *videoConnection = nil; 
        for (AVCaptureConnection *connection in output.connections) { 
         for (AVCaptureInputPort *port in [connection inputPorts]) { 
          if ([[port mediaType] isEqual:AVMediaTypeVideo]) { 
           videoConnection = connection; 
           break; 
          } 
         } 
         if (videoConnection) { break; } 
        } 

        // Finally take the picture 
        if (videoConnection) { 
         [session startRunning]; 

         [output captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { 

          if (imageDataSampleBuffer != NULL) { 
           NSData *imageData = [AVCaptureStillImageOutput 
                jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; 
           UIImage *photo = [[UIImage alloc] initWithData:imageData]; 
           NSLog(@"Captured img====> %@",photo); 
           app.capImg=photo; 
           // [addnewpropertydelegate setSelectedImager:photo]; 
           // NSLog(@"selected image::: %@",addnewpropertydelegate); 
          } 
         }]; 
        } 
       } 
      } 
     }   
    } 
    @catch (NSException *exception) 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Camera" message:@"Camera is not available " delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil]; 
     [alert show]; 
     // [alert release]; 
    } 
} 
+0

동일한 코드를 사용하고 있지만 검은 색 오버레이와 이미지로 이미지를 가져 오는 것이 명확하지 않습니다. 다른 코드도 적용 했습니까? 이걸로 도울 수 있니? –

0

modalViewController를 바로 닫아서 대리자를 호출 할 기회가 없기 때문입니다.

당신은 [imgPicker takePicture]

를 호출해야하고 didFinishPickingMediaWithInfo 방법에 [imgPicker dismissModalViewControllerAnimated:YES]를 호출합니다.

+0

정확히 작동합니다. 하지만 나는 그 사용자가 사진이 캡처되고 있다는 것을 알고 싶지 않습니다. 그래서 [self presentModalViewController : imgPicker animated : YES]; 이제이 기능으로 어떻게 캡처 할 수 있습니까? –

+0

ImagePicker를 제시하지 않으면 불가능합니다. 문서화 된 API에 대해서는 언급하지 않았습니다. – Hisenberg

+0

나는 AVCaptureDevice 프론트 카메라를 사용해 왔으며 끝났습니다. –

관련 문제