我如何在 IOS 中将文本框覆盖在本机相机视图上
How do i overlay a text box over a native camera view in IOS
在执行我正在尝试完成的构建时遇到问题。
我已完成构建 1 和构建 2。
构建 1 - 一个简单的空白屏幕,其中包含两个具有对齐约束的文本框
Build 2 - 当应用程序为 运行 时,它会调用 IOS 设备的原生后置摄像头。
我的第三个构建是将这两个之前的构建合二为一,其中本机摄像头显示实时视频源,上面是两个具有对齐约束的文本框。
我不确定如何将这两段代码放在一起。
我目前拥有的代码:
Build 1 - .h 文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIView *TestTextBox;
@property (weak, nonatomic) IBOutlet UITextView *TestTetBox2;
@end
构建 1 - .m 文件
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize TestTextBox;
@synthesize TestTetBox2;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
构建 2 - .h 文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@end
Build 2 - .m 文件
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
if (self.imageView.image == nil) {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; //Defualts to Native Camera View
imagePickerController.showsCameraControls = NO; //removes camera controlls
[self presentViewController:imagePickerController animated:NO completion:nil];
}
else{
}
}
-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
self.imageView.image = image;
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
您不能使用 UIImagePickerController
在自定义 UIView
中展示相机。您需要使用 AVFoundation
框架在自定义 UIView
.
中呈现相机
在执行我正在尝试完成的构建时遇到问题。
我已完成构建 1 和构建 2。
构建 1 - 一个简单的空白屏幕,其中包含两个具有对齐约束的文本框
Build 2 - 当应用程序为 运行 时,它会调用 IOS 设备的原生后置摄像头。
我的第三个构建是将这两个之前的构建合二为一,其中本机摄像头显示实时视频源,上面是两个具有对齐约束的文本框。
我不确定如何将这两段代码放在一起。
我目前拥有的代码:
Build 1 - .h 文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIView *TestTextBox;
@property (weak, nonatomic) IBOutlet UITextView *TestTetBox2;
@end
构建 1 - .m 文件
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize TestTextBox;
@synthesize TestTetBox2;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
构建 2 - .h 文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@end
Build 2 - .m 文件
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
if (self.imageView.image == nil) {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; //Defualts to Native Camera View
imagePickerController.showsCameraControls = NO; //removes camera controlls
[self presentViewController:imagePickerController animated:NO completion:nil];
}
else{
}
}
-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
self.imageView.image = image;
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
您不能使用 UIImagePickerController
在自定义 UIView
中展示相机。您需要使用 AVFoundation
框架在自定义 UIView
.