iPhone:相机预览覆盖

如何添加覆盖( UIImageView )的相机预览和处理触摸呢?

我以前的尝试做到这一点(例如使用UIImagePickerController和添加图像作为子视图)失败。

本教程解释它: http : //www.musicalgeometry.com/? p= 821

只需在叠加视图中添加UIImage,而不是教程中显示的红色区域。

为您的实施文件:

 - (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; } 

您可能能够直接添加UIImageView作为主窗口的子视图而不是UIImagePicker ,它可能会更好。 只要确保按照正确的顺序添加它们,或者打电话

 [window bringSubviewToFront:imageView]; 

照相机启动后。

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

查看相机覆盖视图(3.1和更高版本中可用)

 @property(nonatomic, retain) UIView *cameraOverlayView