【问题标题】:camera overlay picture相机叠加图片
【发布时间】:2013-03-15 11:05:53
【问题描述】:

编辑 3

好消息和坏消息。好消息是,在 Connections Inspector 中,通过断开覆盖 UIToolbar 并连接 UIImageview,我看到了 theKing,但是 - 坏消息 - 我没有看到我也需要的 UIToolbar。所以现在的问题是,当他/她在这里完成后,用户如何才能回到调用的 VC?工具栏和图像如何成为覆盖的一部分,或者“返回按钮”工具栏可以在非覆盖视图上,还是什么?或者如何在 OverlayViewController 上同时显示工具栏和图像?

编辑 3

编辑 2

setupImagePickerOverlayViewController.m

- (void)setupImagePicker:(UIImagePickerControllerSourceType)sourceType
{
    self.imagePickerController.sourceType = sourceType;

    if (sourceType == UIImagePickerControllerSourceTypeCamera)
    {
        // user wants to use the camera interface
        //
        self.imagePickerController.showsCameraControls = NO;

        if ([[self.imagePickerController.cameraOverlayView subviews] count] == 0)
        {
            // setup our custom overlay view for the camera
            //
            // ensure that our custom view's frame fits within the parent frame
            CGRect overlayViewFrame = self.imagePickerController.cameraOverlayView.frame;
            CGRect newFrame = CGRectMake(0.0,
                                         CGRectGetHeight(overlayViewFrame) -
                                         self.view.frame.size.height - 10.0,
                                         CGRectGetWidth(overlayViewFrame),
                                         self.view.frame.size.height + 10.0);
            self.view.frame = newFrame;
            [self.imagePickerController.cameraOverlayView addSubview:self.view];
        }
    }
}

编辑 2

编辑 1

This is the PhotoPicker sample code link.

编辑 1

编辑 0

有没有人在 iPhone 而不是 iPad 上试过这个?我没有 iPhone,但我今天正在阅读 Matt Neuburg 的书,他说 UIImagePicker 在这两种设备上的工作方式不同。

编辑 0

我在 UIImagePicker 中看不到我试图覆盖在相机视图中的图像。无论我如何添加 IBOutlet,作为属性,图像都不会显示,但覆盖的工具栏显示正常。为什么?

将带有theKingIBOutlet 的OverlayViewController.h 添加到Apple 的示例代码中,然后暂时注释掉。

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioServices.h>

@protocol OverlayViewControllerDelegate;

@interface OverlayViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
    // IBOutlet UIImageView* theKing;   ******** temporarily commented out
    id <OverlayViewControllerDelegate> delegate;
}    

@property (nonatomic, assign) id <OverlayViewControllerDelegate> delegate;
@property (nonatomic, retain) UIImagePickerController *imagePickerController;

- (void)setupImagePicker:(UIImagePickerControllerSourceType)sourceType;

@end

@protocol OverlayViewControllerDelegate
- (void)didTakePicture:(UIImage *)picture;
- (void)didFinishWithCamera;
@end

在 Apple 的示例代码中添加了带有 propertytheKing IBOutlet 的 OverlayViewController.m。

#import "OverlayViewController.h"

enum
{
    kOneShot,       // user wants to take a delayed single shot
    kRepeatingShot  // user wants to take repeating shots
};

@interface OverlayViewController ( )

@property (assign) SystemSoundID tickSound;

@property (nonatomic, retain) IBOutlet UIImageView* theKing;  // added *********
@property (nonatomic, retain) IBOutlet UIBarButtonItem *takePictureButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *startStopButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *timedButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *cancelButton;

@property (nonatomic, retain) NSTimer *tickTimer;
@property (nonatomic, retain) NSTimer *cameraTimer;

当我在 iPhone 上执行代码时,我看不到图像 theKing。下面是我添加的笔尖视图,显示了一些连接。没有抛出错误,但我看不到图像,只有 UIToolbar 已经添加。

【问题讨论】:

    标签: iphone ios xcode uiimagepickercontroller


    【解决方案1】:

    更新

    我现在已对其进行了更新,并尝试复制您在问题中所做的事情。我已经包含了 .h/.m 和 theKing@2x.png 的完整来源。创建一个新项目并将源代码粘贴到文件中并添加theKing@2x.png。一切都以编程方式完成 - 除了将视图嵌入导航控制器之外,您无需在界面构建器中进行任何设置。

    这里是theKing@2x.png - http://i.imgur.com/0DrM7si.png

    ViewController.h

    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> 
    
    @end
    

    ViewController.m

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // assign action to button
        UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        myButton.frame = CGRectMake(0, 0, 200, 60);
        myButton.center = self.view.center;
        [myButton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
        [myButton setTitle:@"Image Picker" forState:UIControlStateNormal];
    
        [self.view addSubview:myButton];
    }
    
    - (void)buttonPress:(id)sender {
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    
            // alert the user that the camera can't be accessed
            UIAlertView *noCameraAlert = [[UIAlertView alloc] initWithTitle:@"No Camera" message:@"Unable to access the camera!" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
            [noCameraAlert show];
    
        } else {
    
            // prepare imagePicker view
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            imagePicker.delegate = self;
            imagePicker.allowsEditing = NO;
            imagePicker.showsCameraControls = NO;
    
            // create view for overlay
            CGRect overlayRect = CGRectMake(0, 0, imagePicker.view.frame.size.width, imagePicker.view.frame.size.height);
            UIView *overlayView = [[UIView alloc] initWithFrame:overlayRect];
    
            // prepare the image to overlay
            UIImageView *overlayImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"theKing"]];
            overlayImage.center = overlayView.center;
            overlayImage.alpha = 0.5;
    
            // prepare toolbar for overlay
            UIToolbar *overlayToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 600, overlayView.frame.size.width, 40)];
            overlayToolbar.center = CGPointMake(overlayView.center.x, overlayView.frame.size.height - 20);
            overlayToolbar.barStyle = UIBarStyleBlack;
    
            UIBarButtonItem *takePictureButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(takePictureButtonPressed:)];
            UIBarButtonItem *flexibleBarSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
            flexibleBarSpace.width = 1000;
            UIBarButtonItem *startStopButton = [[UIBarButtonItem alloc] initWithTitle:@"Snap" style:UIBarButtonItemStyleBordered target:self action:@selector(startStopButtonPressed:)];
            UIBarButtonItem *timedButton = [[UIBarButtonItem alloc]initWithTitle:@"Timed" style:UIBarButtonItemStyleBordered target:self action: @selector(timedButtonPressed:)];
            UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action: @selector(cancelButtonPressed:)];
    
            overlayToolbar.items = [NSArray arrayWithObjects:takePictureButton, flexibleBarSpace, startStopButton, timedButton, cancelButton, nil];
    
    
            [overlayView addSubview:overlayImage];
            [overlayView addSubview:overlayToolbar];
    
            // add the image as the overlay
            [imagePicker setCameraOverlayView:overlayView];
    
            // display imagePicker
            [self.navigationController presentViewController:imagePicker animated:YES completion:nil];
        }
    }
    
    #pragma mark - UIBarButton Selectors
    
    - (void)takePictureButtonPressed:(id)sender {
        NSLog(@"takePictureButtonPressed...");
        // TODO: take picture!
    }
    
    - (void)startStopButtonPressed:(id)sender {
        NSLog(@"startStopButtonPressed...");
        // TODO: make this do something
    }
    
    - (void)timedButtonPressed:(id)sender {
        NSLog(@"timedButtonPressed...");
        // TODO: implement timer before calling takePictureButtonPressed
    }
    
    - (void)cancelButtonPressed:(id)sender {
        NSLog(@"cancelButtonPressed");
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
    
    #pragma mark - UIImagePickerController Delegate Methods
    
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)editingInfo {
    
        // determine if the user selected or took a new photo
        UIImage *selectedImage;
        if ([editingInfo objectForKey:UIImagePickerControllerOriginalImage]) selectedImage = (UIImage *)[editingInfo objectForKey:UIImagePickerControllerOriginalImage];
        else if ([editingInfo objectForKey:UIImagePickerControllerEditedImage]) selectedImage = (UIImage *)[editingInfo objectForKey:UIImagePickerControllerEditedImage];
    
        // TODO: Do something with selectedImage (put it in a UIImageView
    
        // dismiss the imagePicker
        [picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    

    截图

    这就是我运行它时的样子。

    这是否满足您的应用要求?

    【讨论】:

    • 抱歉这么久才回复。到目前为止,我只遇到了“选择器setCameraOverlayView 没有已知类”的错误,并且由于其他义务,我无法研究该问题。你能看看-OverlayViewController.m 中的setupImagePicker: 吗?我会把它添加到问题中。谢谢你的回答。
    • 你能压缩并发布你的源代码吗?今晚我可以看看。我今天大部分时间都处于 AFK 状态,但是当我今晚回到家时,我会很乐意看看。
    • 我想我要到周日晚上才能使用我的电脑。到时候我会压缩并发送给你。谢谢。
    • 我现在已经更新了它,并试图复制你在你的问题中所做的事情。我已经包含了 .h/.m 和 theKing@2x.png 的完整来源。创建一个新项目并粘贴此代码。一切都以编程方式完成 - 您无需在界面生成器中设置任何内容。这满足了您的问题吗?
    • 移除按钮?删除viewDidLoad中的按钮相关代码,然后将代码从buttonPress移动到viewDidLoad。或者,您也可以在viewDidLoad 中使用[self buttonPress:self] 调用buttonPress
    【解决方案2】:

    我找到的一个答案是将图像放在一个巨大的按钮上,该按钮具有Done: 的动作。我必须想办法告诉我的用户这样做,但至少这会关闭图像视图并完成我的设置模式。我真的很想要一个更干净的选择,所以如果你有答案,请添加答案。感谢所有考虑过我的问题的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-25
      • 2021-08-24
      • 1970-01-01
      相关资源
      最近更新 更多