1

我正在尝试从以编程方式生成的 UIView 显示 UIImagePicker,该 UIView 添加为原始视图控制器的子视图。图像选择器出现了,相机也出现了,但是功能中断并且没有正确导航。如何从 UIView 而不是 UIControlView 正确加载图像选择器?

破坏的代码:

- (void)captureImage:(id)sender
{
    [[[UIActionSheet alloc] initWithTitle:nil
                                 delegate:self
                        cancelButtonTitle:@"Close"
                   destructiveButtonTitle:nil
                        otherButtonTitles:@"Take photo", @"Camera Roll", nil]
     showInView:self];
}

//UIImagePicker specific methods
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex)
    {
        case 0:
            [self takePhoto];
                break;

        case 1:
            [self fromCameraRoll];
                break;
    }
}

-(void)takePhoto
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES)
    {
        // Create image picker controller
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

        // Set source to the camera
        imagePicker.sourceType =  UIImagePickerControllerSourceTypeCamera;

        // Delegate is self
        imagePicker.delegate = self;

        // Show image picker
        [self addSubview:imagePicker.view];
    }
}

-(void)fromCameraRoll
{
    UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
    imgPicker.delegate = self;
    imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self addSubview:imgPicker.view];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // Access the uncropped image from info dictionary
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    //do something with the image

    //add code to pick images here and store them in the preview
    [picker dismissModalViewControllerAnimated:NO];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissModalViewControllerAnimated:NO];
}
4

1 回答 1

3

UIImagePickerController 是视图控制器类,您最好通过委托调用或类似覆盖子视图的方式将图像选择器呈现给父视图控制器以实现您的目标。

UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
imgPicker.delegate = self;
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

CustomOverlayView *overlayview = [[CustomOverlayView alloc] init];

imgPicker.cameraOverlayView = overlayview;

[self presentModelViewController:imgPicker animated:YES];

在您的覆盖类中添加另一个委托以关闭您的相机选择器控制器视图

[imgPicker dismissModelViewControllerAnimated:YES];
于 2013-05-09T04:26:11.313 回答