我正在为图像选择器控制器创建完全自定义的叠加层。第一阶段很简单:我第一次看到记录/拍摄按钮,调用takePicture()
方法。
下一步是将UI更改为“重拍”或“使用照片”,这也是我自己的设计。可以通过再次呼叫presentViewController
来模仿“重拍”。我该如何模仿“使用照片”按钮?
答案 0 :(得分:8)
您可以轻松继承UIImagePickerController
<强>目标C 强>
@interface CameraViewController : UIImagePickerController
@property (readwrite, nonatomic) NSInteger state; // 0 - auto 1 - on 2 - off
@end
@implementation CameraViewController
- (instancetype)init {
self = [super init];
if (self) {
if (!TARGET_IPHONE_SIMULATOR) {
self.sourceType = UIImagePickerControllerSourceTypeCamera;
self.showsCameraControls = NO;
self.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
} else {
self.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
self.modalPresentationStyle = UIModalPresentationFullScreen;
self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
self.allowsEditing = NO;
self.mediaTypes = @[(NSString *)kUTTypeImage];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
然后通过创建viewDidLoad
对象并将其设置为UIView
来覆盖self.cameraOverlayView
并根据需要自定义UI。在我的例子中,它是自定义flash按钮。
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
view.backgroundColor = [UIColor clearColor];
UIButton *changeFlashButton = [UIButton buttonWithType:UIButtonTypeCustom];
changeFlashButton.frame = CGRectMake(70, 27, 40, 40);
[changeFlashButton setImage:[UIImage imageNamed:@"flash_auto"] forState:UIControlStateNormal];
self.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
changeFlashButton.showsTouchWhenHighlighted = YES;
[changeFlashButton addTarget:self action:@selector(changeFlashValue:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:changeFlashButton];
if (!TARGET_IPHONE_SIMULATOR) {
self.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
changeFlashButton.alpha = [[self class] isFlashAvailableForCameraDevice:self.cameraDevice];
self.cameraOverlayView = view;
}
}
- (void)changeFlashValue:(UIButton*)aSwitch {
if (self.state == 0) { //auto - on
self.state = 1;
[self.flashSwitch setImage:[UIImage imageNamed:@"flash_on"] forState:UIControlStateNormal];
self.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
return;
}
if (self.state == 1) { //on - off
self.state = 2;
[self.flashSwitch setImage:[UIImage imageNamed:@"flash_off"] forState:UIControlStateNormal];
self.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
return;
}
if (self.state == 2) { //off - auto
self.state = 0;
[self.flashSwitch setImage:[UIImage imageNamed:@"flash_auto"] forState:UIControlStateNormal];
self.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
return;
}
}
请注意不要向用户显示&#34;重拍&#34;和&#34;使用照片&#34;您需要将allowsEditing
设置为false
Swift 2.2 +
实际上是一样的class CameraViewController: UIImagePickerController {
var state: Int?
init() {
super.init(navigationBarClass: nil, toolbarClass: nil)
#if (arch(i386) || arch(x86_64)) && (os(iOS) || os(watchOS) || os(tvOS)) // any simulator
self.sourceType = .PhotoLibrary
#else
self.sourceType = .Camera
self.showsCameraControls = false
self.cameraCaptureMode = .Photo
#endif
self.modalPresentationStyle = .FullScreen
self.modalTransitionStyle = .CrossDissolve
self.allowsEditing = false
self.mediaTypes = [UIImagePickerControllerMediaType]
UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(orientationChanged), name: UIDeviceOrientationDidChangeNotification, object: nil)
}
// strange compiler requirements
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func orientationChanged() {
// update to the new orientation
}
override func viewDidLoad() {
super.viewDidLoad()
let view: UIView = UIView(frame: self.view.bounds)
view.backgroundColor = UIColor.clearColor()
var changeFlashButton: UIButton = UIButton(type: .Custom)
changeFlashButton.frame = CGRectMake(70, 27, 40, 40)
changeFlashButton.setImage(UIImage(named: "flash_auto"), forState: .Normal)
self.cameraFlashMode = .Auto
changeFlashButton.showsTouchWhenHighlighted = true
changeFlashButton.addTarget(self, action: #selector(changeFlashValue(_:)), forControlEvents: .TouchUpInside)
view.addSubview(changeFlashButton)
#if (arch(i386) || arch(x86_64)) && (os(iOS) || os(watchOS) || os(tvOS)) // any simulator
#else
self.cameraFlashMode = .Auto
changeFlashButton.alpha = UIImagePickerController.isFlashAvailableForCameraDevice(self.cameraDevice) ? 1 : 0
#endif
}
func changeFlashValue(aSwitch: UIButton) {
if self.state == 0 {
// auto - on
self.state = 1
self.flashSwitch.setImage(UIImage.imageNamed("flash_on"), forState: .Normal)
self.cameraFlashMode = .On
return
}
if self.state == 1 {
// on - off
self.state = 2
self.flashSwitch.setImage(UIImage.imageNamed("flash_off"), forState: .Normal)
self.cameraFlashMode = .Off
return
}
if self.state == 2 {
// off - auto
self.state = 0
self.flashSwitch.setImage(UIImage.imageNamed("flash_auto"), forState: .Normal)
self.cameraFlashMode = .Auto
return
}
}
}
答案 1 :(得分:4)
You can imitate use photo by presenting a view controller that gives user option to edit the taken image and adding save and cancel button as well. How does that sound?