UIView子视图中的IOS UIImagePicker

Posted

技术标签:

【中文标题】UIView子视图中的IOS UIImagePicker【英文标题】:IOS UIImagePicker in a UIView subview 【发布时间】:2013-05-08 16:57:43 【问题描述】:

我正在尝试从以编程方式生成的 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];

【问题讨论】:

在 iPhone 上,您应该使用 presentViewController:animated:completion: 以模态方式(全屏)呈现图像选择器。我不知道苹果是否会同意从较小的角度展示它。 【参考方案1】:

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

【讨论】:

只是一个简短的说明,最好使用 [self presentViewController:imagePicker animated:YES completion:nil];和 [picker dismissViewControllerAnimated:YES 完成:nil];因为 presentModelViewController 和 dismissModelViewControllerAnimated 已被弃用

以上是关于UIView子视图中的IOS UIImagePicker的主要内容,如果未能解决你的问题,请参考以下文章

如何在iOS中的UIView子视图下方插入UILabel

从 iOS 中的 UIView 子视图重新加载时 UITableView 未重新加载

有没有办法将 UIView 嵌入到 UIScrollview 中而不会丢失 iOS 中的子视图约束?

iOS 7,UIView 子视图为零

如何在iOS中查找UIView子视图的值变化

iOS:处理 UI(子)视图中的 UIGestureRecognisers