问题描述
我注意到,在OS 3.1中,您可以使用
向图片选择器添加重叠视图。 cameraOverlayView
但是,我也注意到通过这个方法添加视图也显示了整个UIImagePicker的视图
是否有一种方法使它只在预览期间发生?
解决方案 如果您想要使用图片或重新拍摄, div> 我想出了一个方法来实现想要的结果,虽然它有点...不那么标准。 :)
这个想法是在结构中重新排列内部视图的顺序 UIImagePickerController
使用。 / p>
OK,所以我们创建一个 UIImagePickerController
对象,初始化它并添加一个重叠视图。我可以请你注意! UIView
对象(示例代码中的 UIImageView
)从一开始就隐藏。不要错过。最后,我们将图像拾取器控制器作为模态视图控制器。此代码应该在您的 applicationDidFinishLaunching:
, viewWillAppear:
或类似的适当的启动方法。
UIImagePickerController * anImagePickerController = [UIImagePickerController new];
anImagePickerController.delegate = self;
anImagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
UIImageView * anImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@Watermark.png]];
anImageView.frame = CGRectMake(0,1,anImageView.image.size.width,aImageView.image.size.height);
anImageView.hidden = YES;
anImagePickerController.cameraOverlayView = anImageView;
[viewController presentModalViewController:anImagePickerController animated:NO];
[anImagePickerController release];
[NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(timerFireMethod :)
userInfo:anImageView
repeats:YES];
[anImageView release];
在叠加视图( anImageView
)之前释放创建一个 NSTimer
,初始化为 anImageView
( NSTimer
userInfo
属性)并立即计划。这里是它调用的方法:
- (void)timerFireMethod:(NSTimer *)theTimer {
UIView * cameraOverlayView = (UIView *)theTimer.userInfo;
UIView * previewView = cameraOverlayView.superview.superview;
if(previewView!= nil){
[cameraOverlayView removeFromSuperview];
[previewView insertSubview:cameraOverlayView atIndex:1];
cameraOverlayView.hidden = NO;
[theTimer invalidate];
}
}
整个 NSTimer 事件被添加到流中,以确保当 UIImagePickerController
完全准备就绪时,重新排序工作将会发生。
这是它。它工作,它不是标准,它的粗糙和快速。再次感到自由,优化,使它更好(哦,请做,我的目标是告诉你的方式)。
I've noticed that in OS 3.1 you can add an overlay view to the image picker with
cameraOverlayView
However, I've also noticed that adding a view via this method also displays the view for the entire time the UIImagePicker is displayed, when I only want to show it during the preview stage.
Is there a way to make this only happen during preview? I don't want it to be there during the open shutter animation, or the screen that asks whether you want to use the image or retake.
解决方案 I've figured out a way for you to achieve the desired result though it's a bit... not so standard. :)
The idea is to rearrange a bit the order of the inner views in the structure UIImagePickerController
uses.
OK, so we create an UIImagePickerController
object, initialize it and add an overlay view to it. May I have your attention please! The UIView
object (UIImageView
in the example code) is hidden from the very beginning. Don't miss that. Finally we present the image picker controller as modal view controller. This code should be somewhere in your applicationDidFinishLaunching:
, viewWillAppear:
or similar appropriate about to launch methods.
UIImagePickerController *anImagePickerController = [UIImagePickerController new];
anImagePickerController.delegate = self;
anImagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
UIImageView *anImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Watermark.png"]];
anImageView.frame = CGRectMake(0, 1, anImageView.image.size.width, anImageView.image.size.height);
anImageView.hidden = YES;
anImagePickerController.cameraOverlayView = anImageView;
[viewController presentModalViewController:anImagePickerController animated:NO];
[anImagePickerController release];
[NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(timerFireMethod:)
userInfo:anImageView
repeats:YES];
[anImageView release];
Before the overlay view (anImageView
) is released a NSTimer
is created, initialized with anImageView
(NSTimer
userInfo
property) and scheduled right away. Here's the method it calls:
- (void)timerFireMethod:(NSTimer*)theTimer {
UIView *cameraOverlayView = (UIView *)theTimer.userInfo;
UIView *previewView = cameraOverlayView.superview.superview;
if (previewView != nil) {
[cameraOverlayView removeFromSuperview];
[previewView insertSubview:cameraOverlayView atIndex:1];
cameraOverlayView.hidden = NO;
[theTimer invalidate];
}
}
The whole NSTimer
thing is added to the flow to ensure that the reordering work around will happen exactly when the UIImagePickerController
will be totally ready for that.
This is it. It works, it's not standard, it's rough and quick. Again feel free to optimize and make it 'righter' (oh please do, my aim was to show you the way).
这篇关于相机重叠视图 - 只是为了预览?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!