UIImagePickerController不显示编辑屏幕

时间:2013-07-10 18:07:50

标签: iphone ios objective-c camera uiimagepickercontroller

我正在使用cameraOverLayView并遇到一种奇怪的行为。在呈现UIImagePickerController之前,我将allowsEditing设置为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];

}

1 个答案:

答案 0 :(得分:0)

可能的dublicate:How do I use [camera takePhoto] and edit using UIImagePicker - UIImagePickerController.allowsEditing = YES

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

  

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

     

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

似乎这种方法(自定义叠加)是为了自己管理而配置的。即使“allowsEditing = YES”,拍摄的照片也会直接发送到imagePickerController:didFinishPickingMediaWithInfo:。

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