iPhone:相机预览叠加

发布于 2024-07-24 21:57:21 字数 127 浏览 0 评论 0原文

如何将叠加层 (UIImageView) 添加到相机预览并 处理这个问题吗?

我之前尝试执行此操作(例如使用 UIImagePickerController 并将图像添加为子视图)失败了。

How do I add an overlay (UIImageView) to the camera preview and
handle touches on this?

My previous attempts to do this (e.g. use UIImagePickerController and add the image as a subview) have failed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伴我老 2024-07-31 21:57:21

对于您的实现文件:

- (IBAction)TakePicture:(id)sender {

    // Create image picker controller
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

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

        // Delegate is self
        imagePicker.delegate = self;

        OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];     

        // Insert the overlay:
        imagePicker.cameraOverlayView = overlay;

       // Allow editing of image ?
        imagePicker.allowsImageEditing = YES;
        [imagePicker setCameraDevice:
        UIImagePickerControllerCameraDeviceFront];
        [imagePicker setAllowsEditing:YES];
        imagePicker.showsCameraControls=YES;
        imagePicker.navigationBarHidden=YES;
        imagePicker.toolbarHidden=YES;
        imagePicker.wantsFullScreenLayout=YES;

        self.library = [[ALAssetsLibrary alloc] init];

        // Show image picker
        [self presentModalViewController:imagePicker animated:YES];
    }

创建一个 UIView 类并添加此代码

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code


            // Clear the background of the overlay:
            self.opaque = NO;
            self.backgroundColor = [UIColor clearColor];

            // Load the image to show in the overlay:
            UIImage *overlayGraphic = [UIImage imageNamed:@"overlaygraphic.png"];
            UIImageView *overlayGraphicView = [[UIImageView alloc] initWithImage:overlayGraphic];
            overlayGraphicView.frame = CGRectMake(30, 100, 260, 200);
            [self addSubview:overlayGraphicView];

        }
        return self;
    }

For your implementation file:

- (IBAction)TakePicture:(id)sender {

    // Create image picker controller
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

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

        // Delegate is self
        imagePicker.delegate = self;

        OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];     

        // Insert the overlay:
        imagePicker.cameraOverlayView = overlay;

       // Allow editing of image ?
        imagePicker.allowsImageEditing = YES;
        [imagePicker setCameraDevice:
        UIImagePickerControllerCameraDeviceFront];
        [imagePicker setAllowsEditing:YES];
        imagePicker.showsCameraControls=YES;
        imagePicker.navigationBarHidden=YES;
        imagePicker.toolbarHidden=YES;
        imagePicker.wantsFullScreenLayout=YES;

        self.library = [[ALAssetsLibrary alloc] init];

        // Show image picker
        [self presentModalViewController:imagePicker animated:YES];
    }

Make a UIView class and add this code

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code


            // Clear the background of the overlay:
            self.opaque = NO;
            self.backgroundColor = [UIColor clearColor];

            // Load the image to show in the overlay:
            UIImage *overlayGraphic = [UIImage imageNamed:@"overlaygraphic.png"];
            UIImageView *overlayGraphicView = [[UIImageView alloc] initWithImage:overlayGraphic];
            overlayGraphicView.frame = CGRectMake(30, 100, 260, 200);
            [self addSubview:overlayGraphicView];

        }
        return self;
    }
不顾 2024-07-31 21:57:21

您可以直接将 UIImageView 添加为主窗口的子视图,而不是 UIImagePicker,这样效果可能会更好。 只需确保按正确的顺序添加它们,或者

[window bringSubviewToFront:imageView];

在相机启动后致电即可。

如果您想处理 UIImageView 上的触摸,只需将 UIImageView 添加为具有透明背景的普通全屏 View 的子视图,然后将 that 添加到窗口,并使用可用于处理触摸事件的普通 UIViewController 子类。

You may be able to add the UIImageView as a subview of the main window directly instead of the UIImagePicker, it may work better. Just make sure to add them in the right order, or call

[window bringSubviewToFront:imageView];

after the camera is up.

If you want to handle touches on the UIImageView you could just add the UIImageView as a subview of a normal fullscreen View with a transparent background, and add that to the window instead, with a normal UIViewController subclass that you can use to handle the touch events.

滥情稳全场 2024-07-31 21:57:21

查看相机叠加视图(3.1 及更高版本中提供)

@property(nonatomic, retain) UIView *cameraOverlayView

See the camera overlay view (available in 3.1 and later)

@property(nonatomic, retain) UIView *cameraOverlayView
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文