1

我正在玩 cameraOverLayView 并遇到了一个奇怪的行为。在展示 UIImagePickerController 之前,我将allowEditing 设置为YES。捕获屏幕出现后,我点击触发 takePicture() 的按钮。委托方法 didFinishPickingMediaWithInfo() 会立即调用,而不是显示编辑屏幕。谁能帮我弄清楚我做错了什么?我在下面粘贴了一些代码...

谢谢!

    - (BOOL)shouldStartCameraController {

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO) {
        return NO;
    }

    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    float overlayOffset = 0;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        if (screenSize.height > 480.0f) {
            overlayOffset = 195;
        } else {
            overlayOffset = 103;
        }
    } else {
        /*Do iPad stuff here.*/
    }


    UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]
        && [[UIImagePickerController availableMediaTypesForSourceType:
             UIImagePickerControllerSourceTypeCamera] containsObject:(NSString *)kUTTypeImage]) {

        cameraUI.mediaTypes = [NSArray arrayWithObject:(NSString *) kUTTypeImage];
        cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;

        if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
            cameraUI.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        } else if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {
            cameraUI.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        }

    } else {
        return NO;
    }


    UIView *cameraOverlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cameraUI.view.frame.size.width, cameraUI.view.frame.size.height)];
    UIImageView *cameraOverlayImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iphone5_camera_overlay.png"]];
    cameraOverlayImageView.frame = CGRectMake(0, 0, cameraUI.view.frame.size.width, cameraUI.view.frame.size.height);
    [cameraOverlayView addSubview:cameraOverlayImageView];


    UILabel *cameraLabel = [[UILabel alloc] initWithFrame:CGRectMake( 0.0f, self.view.bounds.size.height-overlayOffset, self.view.bounds.size.width, 50.0f)];
    [cameraLabel setTextAlignment:NSTextAlignmentCenter];
    [cameraLabel setBackgroundColor:[UIColor clearColor]];
    [cameraLabel setTextColor:[UIColor whiteColor]];
    [cameraLabel setShadowColor:[UIColor colorWithWhite:0.0f alpha:0.300f]];
    [cameraLabel setShadowOffset:CGSizeMake( 0.0f, -1.0f)];
    [cameraLabel setFont:[UIFont boldSystemFontOfSize:18.0f]];
    [cameraOverlayView addSubview:cameraLabel];

    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton addTarget:self action:@selector(cancelButtonPressed:) forControlEvents:UIControlEventTouchDown];
    [cancelButton setFrame:CGRectMake(10, cameraOverlayView.frame.size.height-60, 50, 50)];
    [cancelButton setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0.5f]];
    [cameraOverlayView addSubview:cancelButton];


    UIButton *snapButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [snapButton addTarget:self action:@selector(takePictureButtonPressed:) forControlEvents:UIControlEventTouchDown];
    [snapButton setFrame:CGRectMake(110, cameraOverlayView.frame.size.height-60, 100, 50)];
    [snapButton setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0.5f]];
    [cameraOverlayView addSubview:snapButton];
    cameraUI.allowsEditing = YES;
    cameraUI.showsCameraControls = NO;
    cameraUI.delegate = self;
    self.imagePickerController = cameraUI;

    [self presentModalViewController:cameraUI animated:YES];

    return YES;
}


- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer {
    [self shouldPresentPhotoCaptureController];
}

#pragma mark - UIBarButton Selectors

- (void)takePictureButtonPressed:(id)sender {
    NSLog(@"takePictureButtonPressed...");
    // TODO: take picture!
    [self.imagePickerController takePicture];

}
4

1 回答 1

0

可能重复:如何使用 [camera takePhoto] 并使用 UIImagePicker 进行编辑 - UIImagePickerController.allowsEditing = YES

根据 Apple 文档中的 takePicture: 方法:

将此方法与自定义叠加视图结合使用,以启动静止图像的编程捕获。这支持在不离开界面的情况下拍摄多张照片,但需要您隐藏默认的图像选择器控件。

在捕获图像时调用此方法无效。您必须等到关联的委托对象收到 imagePickerController:didFinishPickingMediaWithInfo: 消息后才能捕获另一张图片。

似乎这种方法(自定义覆盖)被配置为由您自己管理。即使 "allowsEditing = YES" 拍摄的图片也会直接发送到 imagePickerController:didFinishPickingMediaWithInfo:。

基于此,如果我们想使用我们的自定义用户界面编辑拍摄的照片,我们应该为此创建一个相应的自定义编辑屏幕。

于 2013-07-31T04:27:29.260 回答