iPhone SDK - 如何在 UIImagePickerController 中禁用图片预览?

Posted

技术标签:

【中文标题】iPhone SDK - 如何在 UIImagePickerController 中禁用图片预览?【英文标题】:iPhone SDK - How to disable the picture preview in UIImagePickerController? 【发布时间】:2010-02-20 01:03:11 【问题描述】:

有没有什么办法可以在使用 UIImagePickerController 拍照后禁用图像预览?我想在用户按下快门按钮后立即关闭 ImagePicker。

【问题讨论】:

【参考方案1】:

我问了类似的问题here

我的解决方案是在默认的 UIImagePickerControllerView 之上创建一个自定义视图。

我下载了例子Augmented Reality

然后您可以通过将 OverlayView.m 和 OverlayView.h 添加到您的项目中来使用它们: 我将自定义选取器工具栏、选取器和 overlayView 设为全局,以便我可以在项目中的任何位置访问它们。

在你的 ViewController.h 中

@class OverlayView;

@interface ViewController //bla bla...

UIImagePickerController * picker;
UIToolbar *toolBar;
OverlayView *overlayView; 

我创建了工具栏的控件一个相机按钮和取消按钮

// toolbar - handy if you want to be able to exit from the image picker...
            toolBar=[[[UIToolbar alloc] initWithFrame:CGRectMake(0, 480-55, 320, 55)] autorelease];
            toolBar.barStyle =  UIBarStyleBlackOpaque;
            NSArray *items=[NSArray arrayWithObjects:
                            [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel  target:self action:@selector(cancelPicture)] autorelease],
                            [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  target:nil action:nil] autorelease],
                            [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera  target:self action:@selector(shootPicture)] autorelease],
                            [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  target:nil action:nil] autorelease],
                            [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  target:nil action:nil] autorelease],
                            nil];
            [toolBar setItems:items];

            // create the overlay view
            overlayView=[[[OverlayView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-44)] autorelease];
            // important - it needs to be transparent so the camera preview shows through!
            overlayView.opaque=NO;
            overlayView.backgroundColor=[UIColor clearColor];

                    // parent view for our overlay
        UIView *parentView=[[[UIView alloc] initWithFrame:CGRectMake(0,0,320, 480)] autorelease];
        [parentView addSubview:overlayView];
        [parentView addSubview:toolBar];

        // configure the image picker with our overlay view
        picker=[[UIImagePickerController alloc] init];
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;

        // hide the camera controls
        picker.showsCameraControls=NO;
        picker.wantsFullScreenLayout = YES;

取消方法

- (IBAction)cancel 
    // Don't pass current value to the edited object, just pop.
    [self.navigationController popViewControllerAnimated:YES];

(拍摄图片方法):

-(void) shootPicture 

    [picker takePicture];


要退出而不显示预览,只需在 didFinishPickingImage 方法中拍照后关闭视图

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo 

//do whatever

[self dismissModalViewControllerAnimated:YES];

【讨论】:

这个例子中,抓图的按钮是不是很小? @eratusnjuki 我在使用 iPad 时遇到了问题。问题是,在从纵向到横向重新定位工具栏时,反之亦然,工具栏重新定位效果清晰可见(即工具栏在很短的时间内从一个位置移动到另一个位置)。如何解决这个问题? didFinishPickingImage 现在我认为已弃用 链接已损坏。【参考方案2】:

这是快速版本:

                /// toolbar - handy if you want to be able to exit from the image picker...
            toolBar = UIToolbar(frame: CGRect(x: 0, y: 480 - 55, width: 320, height: 55))
            toolBar?.barStyle = UIBarStyle.black
            
            let items = [
                UIBarButtonItem(barButtonSystemItem: .cancel, target: self,
                                action:#selector(cancelPicture)),
                UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
                UIBarButtonItem(barButtonSystemItem: .camera, target: self,
                                action:#selector(shootPicture))
            ]
            
            toolBar?.items = items

            // create the overlay view
            let overlayView = OverlayView()
            overlayView.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480 - 44)

            // important - it needs to be transparent so the camera preview shows through!
            overlayView.view.isOpaque = false

            picker.view.addSubview(overlayView.view)
            picker.view.addSubview(toolBar!)
            

            // hide the camera controls
            picker.showsCameraControls = false
            picker.cameraOverlayView = overlayView.view
            
            toolBar?.snp.makeConstraints(make) ->  Void in
                make.width.equalTo(180)
                make.height.equalTo(50)
                make.top.equalTo(picker.view.snp.bottom).offset(-200)
                make.centerX.equalTo(picker.view.snp.centerX)
            

【讨论】:

以上是关于iPhone SDK - 如何在 UIImagePickerController 中禁用图片预览?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 iOS 6 sdk 为 iPhone 4s 或 iPhone 4 制作视图控制器?

您如何在 iPhone 上为 PayPal 的电话 SDK 保护您的 PayPal 应用程序 ID?

如何使用最新的 SDK 为原始“iPhone”开发?

如何在 iphone sdk 中合并音频和视频

如何在 iphone sdk 中使用 openlayers

如何在 UItextView 中添加 placeHolder 文本?在 iphone sdk [重复]