如何在Xcode中创build简单的摄像头覆盖?
正如标题所说,我需要创build一个非常简单的相机覆盖,而与UIImagePickerController
拍照。 我想添加非常简单的.png
文件(像一个空框),但我不知道如何做到这一点。
我已经检查了这个网页上的大部分教程,但是大部分都不明白。 这对我来说似乎非常困难,并且将一个简单的.png
文件添加到相机覆盖图应该比这更容易。
(IBAction) getPhoto:(id) sender { UIImagePickerController * picker = [[UIImagePickerController alloc] init]; picker.delegate = self; if((UIButton *) sender == choosePhotoBtn) { picker.sourceType = UIImagePickerControllerSourceTypeCamera; } else { } label1.text =@"PHOTO ACTION"; [self presentModalViewController:picker animated:YES]; } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissModalViewControllerAnimated:YES]; imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; }
我应该在哪里实现我的覆盖代码? 我应该使用哪些课程,我该怎么做?
UIImagePickerController
有一个名为cameraOverlayView
的属性。 这是一个UIView*
,所以你需要创build一个PNG到它的背景,然后把它分配给你的picker
这个属性。
好的,这是代码(我没有testing过)
(IBAction) getPhoto:(id) sender { UIImagePickerController * picker = [[UIImagePickerController alloc] init]; picker.delegate = self; // creating overlayView UIView* overlayView = [[UIView alloc] initWithFrame:picker.view.frame]; // letting png transparency be overlayView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourimagename.png"]]; [overlayView.layer setOpaque:NO]; overlayView.opaque = NO; picker.showsCameraControls = NO; picker.cameraOverlayView = overlayView; if((UIButton *) sender == choosePhotoBtn) { if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { [picker setSourceType:UIImagePickerControllerSourceTypeCamera]; } else { //do something if the device has no camera or if the camera is disabled in settings (it cannot be assumed that the camera is available/not broken) } } else { } label1.text =@"PHOTO ACTION"; [self presentModalViewController:picker animated:YES]; }
当然,你可以使用一些自定义的UIView*
子类,而不是创build标准的UIView* overlayView
,但是其他的东西将保持不变。