像iOS相机应用程序一样显示屏幕闪烁

时间:2014-11-07 02:42:53

标签: ios objective-c iphone swift

我想实现像在原生iOS相机应用程序中完成的一样 - 当我用户拍照时,屏幕会瞬间闪烁一段时间。

我正在使用带叠加层的UIImagePicker,并希望在顶部添加第二个视图。

以下是一些代码:

TOP:

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate,
                  UIGestureRecognizerDelegate {
var cameraUI:UIImagePickerController = UIImagePickerController()
var overlay:UIImageView!

展示相机:

func presentCamera()
{
    cameraUI = UIImagePickerController()
    cameraUI.delegate = self
    cameraUI.sourceType = UIImagePickerControllerSourceType.Camera
    cameraUI.mediaTypes = [kUTTypeImage]
    cameraUI.allowsEditing = false
    cameraUI.showsCameraControls = false

    var translate = CGAffineTransformMakeTranslation(0.0, 71.0); //This slots the preview exactly in the middle of the screen by moving it down 71 points
    cameraUI.cameraViewTransform = translate;

    var scale = CGAffineTransformScale(translate, 1.333333, 1.333333);
    cameraUI.cameraViewTransform = scale;


    self.presentViewController(cameraUI, animated: true, completion: nil)
    overlay = UIImageView(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))
    var image = UIImage(named: "bg.png")
    overlay.image = image
    overlay.userInteractionEnabled = true
    overlay.alpha = 1.0
    cameraUI.cameraOverlayView = overlay

    let recognizer = UITapGestureRecognizer(target: self, action:Selector("tapped:"))
    recognizer.delegate = self
    recognizer.numberOfTapsRequired = 1
    overlay.addGestureRecognizer(recognizer)

}

Flash Screen:用户拍照时调用

func flashScreen() {
    let flashView : UIView = UIView(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))
    flashView.alpha = 0.0
    flashView.backgroundColor = UIColor.blackColor()

    print("flash")

    overlay.addSubview(flashView)
//        self.view.addSubview(flashView) doesn't work either

我什么也看不见

我也尝试过改变叠加图像,这似乎也不起作用:

//        overlay.alpha = 0.0
//        overlay.image = UIImage(named: "flash.png")
//        cameraUI.cameraOverlayView = overlay

1 个答案:

答案 0 :(得分:1)

我注意到flashView.alpha = 0.0,这意味着它是透明的。将其设置为1.0。