在自定义图像选取器覆盖上执行"使用照片"按钮

vla*_*chu 5 uiimagepickercontroller ios swift

我正在为图像选择控制器创建完全自定义的叠加.第一阶段很简单:我得到第一个带记录/拍摄按钮的视图,这是一种调用takePicture()方法.

下一步是将UI更改为"重新拍摄"或"使用照片",这也是我自己的设计.可能可以通过presentViewController再次打电话来模仿"重拍" .我该如何模仿"使用照片"按钮?

ale*_*tro 8

你可以轻松继承 UIImagePickerController

Objective-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
Run Code Online (Sandbox Code Playgroud)

然后viewDidLoad通过创建UIView对象并将其设置为覆盖并自定义UI以满足您的需求self.cameraOverlayView.在我的例子中,它是自定义闪光按钮.

- (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;
    }
}
Run Code Online (Sandbox Code Playgroud)

您对相机的控制可能是这样的 从左到右:闪光灯,拍照,前/后摄像头,照片库

请注意,如果不向用户显示"重新拍摄"和"使用照片"按钮,则需要将其设置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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Avi*_*rma 4

您可以通过提供一个视图控制器来模仿使用照片,该视图控制器为用户提供编辑拍摄图像的选项,并添加保存和取消按钮。听上去怎么样?