【问题标题】:Capture image without user interaction in iphone在 iphone 中无需用户交互即可捕获图像
【发布时间】:2012-12-21 20:50:23
【问题描述】:

我想在没有用户知道的情况下在 iphone 中捕获图像。为此,我使用了自定义叠加层并调用了 [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];

请帮我做。

【问题讨论】:

    标签: iphone uiimagepickercontroller


    【解决方案1】:
        @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];
        }
    }
    

    【讨论】:

    • 我正在使用相同的代码,但获取带有黑色覆盖的图像和图像不清楚。您是否还应用了任何其他代码?你能帮忙吗?
    【解决方案2】:

    这是因为你直接关闭了 modalViewController,所以它没有机会调用委托。

    您应该致电[imgPicker takePicture]

    然后在didFinishPickingMediaWithInfo方法中调用[imgPicker dismissModalViewControllerAnimated:YES]

    【讨论】:

    • 确实有效。但我不希望用户知道照片被捕获。所以我必须评论 [self presentModalViewController:imgPicker animated:YES];现在如何使用此功能进行捕获?
    • 不提供 ImagePicker 是不可能的。至少在任何可用的文档化 API 中都没有提及。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 2016-10-17
    • 2021-03-21
    • 2012-04-02
    • 2017-11-09
    • 1970-01-01
    相关资源
    最近更新 更多