我试图在我的应用上拍摄照片后解除预览。我只想按下快门按钮,然后回到显示UIViewController
的{{1}},而不显示图像预览和“重拍”和“使用照片”按钮。我正在使用此代码:
UIImagePickerController
并在- (IBAction)pushTakePhoto:(id)sender {
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.cameraDevice=UIImagePickerControllerCameraDeviceFront;
UIImage *catEars = [UIImage imageNamed:@"cameraoverlay"];//320*101
UIImageView *overlayImageView = [[UIImageView alloc] initWithImage:catEars];
CGRect screenRect = [[UIScreen mainScreen] bounds];
self.earsX = screenRect.size.width/2-overlayImageView.frame.size.width/2;
self.earsY = 120;
self.earsWidth = overlayImageView.frame.size.width;
self.earsHeight = overlayImageView.frame.size.height;
//bar size is 40 at the top and the bottom one is 101
overlayImageView.frame=CGRectMake(self.earsX, self.earsY, self.earsWidth, self.earsHeight);
[self.picker setCameraOverlayView:overlayImageView];
[self presentViewController:self.picker animated:YES completion:NULL];
}
我正在尝试使用didFinishPickingMediaWIthInfo
但是它没有任何影响。任何人都可以给我一些指示,请问如何解决这个问题?
[self.picker dismissViewControllerAnimated:YES completion:nil];
答案 0 :(得分:1)
在viewDidLoad
中添加此代码 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadPhotoCaptured) name:@"_UIImagePickerControllerUserDidCaptureItem" object:nil];
然后实现此方法
尝试
- (void)loadPhotoCaptured
{
UIImage *img = [[[self allImageViewsSubViews:[[[picker viewControllers]firstObject] view]] lastObject] image];
if (img)
{
[self imagePickerController:nil didFinishPickingMediaWithInfo:[NSDictionary dictionaryWithObject:img forKey:UIImagePickerControllerOriginalImage]];
}
else
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
}
- (NSMutableArray*)allImageViewsSubViews:(UIView *)view
{
NSMutableArray *arrImageViews=[NSMutableArray array];
if ([view isKindOfClass:[UIImageView class]])
{
[arrImageViews addObject:view];
}
else
{
for (UIView *subview in [view subviews])
{
[arrImageViews addObjectsFromArray:[self allImageViewsSubViews:subview]];
}
}
return arrImageViews;
}
答案 1 :(得分:0)
您确定已使用精确签名实施该方法:- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
?
因为[self dismissViewControllerAnimated:YES completion:nil];
和[self.picker dismissViewControllerAnimated:YES completion:nil];
都应该在你的情况下解雇选择控制器。
答案 2 :(得分:0)
想要发布此内容,其他人认为它很有用。 [self.picker dismissViewControllerAnimated:YES completion:nil];
确实有用,必须放在- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
中,但如果您使用默认的相机控件,它将始终显示" Retake"和"使用Photo"在调用之前 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info。解决此问题的方法是设置self.picker.showsCameraControls=NO;
并在UIButton
中添加cameraOverlayView
来调用[self.picker takePicture]
。现在工作。谢谢你们