2012-09-13 34 views
1

我想在iphone中不需要用户知道的情况下捕获图像。为此,我使用了自定义覆盖图并调用了[imagePicker takePicture],但它没有调用didFinishPickingMediaWithInfo方法。任何人都可以帮我解决这个问题吗?在iPhone中无需用户交互捕获图像

代码:

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]

,然后调用[imgPicker dismissModalViewControllerAnimated:YES]didFinishPickingMediaWithInfo方法。

+0

确切地说,它的工作原理。但我不希望那个用户知道照片被捕获。所以我必须评论[self presentModalViewController:imgPicker animated:YES];现在我如何捕获这个功能? –

+0

不可能不显示ImagePicker。至少没有提到可用的任何记录的API。 – Hisenberg

+0

我已经使用AVCaptureDevice前置摄像头并完成了。 –

相关问题