UIView子视图中的IOS UIImagePicker

时间:2013-05-08 16:57:44

标签: iphone ios uiview subview

我试图从程序生成的UIView中显示一个UIImagePicker,它被添加为原始视图控制器的子视图。图像选择器出现,相机也出现了,但功能中断,没有任何正确的导航。如何从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];
}

1 个答案:

答案 0 :(得分:2)

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];