簡體   English   中英

如何打開/關閉 swift 2 的 iPhone 相機閃光燈?

[英]How to turn the iPhone camera flash on/off swift 2?

我正在尋找如何打開/關閉 iPhone 的相機閃光燈,我發現了這個:

@IBAction func didTouchFlashButton(sender: AnyObject) {
    let avDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

// check if the device has torch
if avDevice.hasTorch {
    // lock your device for configuration
    avDevice.lockForConfiguration(nil)
    // check if your torchMode is on or off. If on turns it off otherwise turns it on
    if avDevice.torchActive {
        avDevice.torchMode = AVCaptureTorchMode.Off
    } else {
        // sets the torch intensity to 100%
        avDevice.setTorchModeOnWithLevel(1.0, error: nil)
    }
    // unlock your device
    avDevice.unlockForConfiguration()
    }
}

我確實有兩個問題,一個在線:

avDevice.lockForConfiguration(nil)

另一個在線:

avDevice.setTorchModeOnWithLevel(1.0, error:nil)

它們都與異常處理有關,但我不知道如何解決它們。

@IBAction func didTouchFlashButton(sender: UIButton) {
    let avDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

    // check if the device has torch
    if avDevice.hasTorch {
        // lock your device for configuration
        do {
            let abv = try avDevice.lockForConfiguration()
        } catch {
            print("aaaa")
        }

        // check if your torchMode is on or off. If on turns it off otherwise turns it on
        if avDevice.torchActive {
            avDevice.torchMode = AVCaptureTorchMode.Off
        } else {
            // sets the torch intensity to 100%
            do {
                let abv = try avDevice.setTorchModeOnWithLevel(1.0)
            } catch {
                print("bbb")
            }
        //    avDevice.setTorchModeOnWithLevel(1.0, error: nil)
        }
        // unlock your device
        avDevice.unlockForConfiguration()
    }
}

Swift 4 版本,改編自 Ivan Slavov 的回答。 “TorchMode.auto”也是一個選項,如果你想變得有趣。

    @IBAction func didTouchFlashButton(_ sender: Any) {
    if let avDevice = AVCaptureDevice.default(for: AVMediaType.video) {
        if (avDevice.hasTorch) {
            do {
                try avDevice.lockForConfiguration()
            } catch {
                print("aaaa")
            }

            if avDevice.isTorchActive {
                avDevice.torchMode = AVCaptureDevice.TorchMode.off
            } else {
                avDevice.torchMode = AVCaptureDevice.TorchMode.on
            }
        }
        // unlock your device
        avDevice.unlockForConfiguration()
    }
}

出於某種原因,“avDevice.torchActive”始終為假,即使手電筒打開,也無法關閉,但我通過聲明一個最初設置為 false 的布爾值來修復它,並且每次閃光燈打開時,布爾值都設置為真的。

var on: Bool = false    

@IBAction func didTouchFlashButton(sender: UIButton) {
let avDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

// check if the device has torch
if avDevice.hasTorch {
    // lock your device for configuration
    do {
        let abv = try avDevice.lockForConfiguration()
    } catch {
        print("aaaa")
    }

    // check if your torchMode is on or off. If on turns it off otherwise turns it on
    if on == true {
        avDevice.torchMode = AVCaptureTorchMode.Off
        on = false
    } else {
        // sets the torch intensity to 100%
        do {
            let abv = try avDevice.setTorchModeOnWithLevel(1.0)
            on = true
        } catch {
            print("bbb")
        }
    //    avDevice.setTorchModeOnWithLevel(1.0, error: nil)
    }
    // unlock your device
    avDevice.unlockForConfiguration()
}

}

import AVFoundation

var videoDeviceInput: AVCaptureDeviceInput?
var movieFileOutput: AVCaptureMovieFileOutput?
var stillImageOutput: AVCaptureStillImageOutput?

向 ViewController 添加一個類方法。

class func setFlashMode(flashMode: AVCaptureFlashMode, device: AVCaptureDevice){

        if device.hasFlash && device.isFlashModeSupported(flashMode) {
            var error: NSError? = nil
            do {
                try device.lockForConfiguration()
                device.flashMode = flashMode
                device.unlockForConfiguration()
            } catch let error1 as NSError {
                error = error1
                print(error)
            }
        }

    }

檢查 flashmode 狀態。

// Flash set to Auto/Off for Still Capture
                print("flashMode.rawValue : \(self.videoDeviceInput!.device.flashMode.rawValue)")
                if(self.videoDeviceInput!.device.flashMode.rawValue == 1)
                {
                    CameraViewController.setFlashMode(AVCaptureFlashMode.On, device: self.videoDeviceInput!.device)
                }
                else if (self.videoDeviceInput!.device.flashMode.rawValue == 2)
                {
                    CameraViewController.setFlashMode(AVCaptureFlashMode.Auto, device: self.videoDeviceInput!.device)
                }
                else
                {
                    CameraViewController.setFlashMode(AVCaptureFlashMode.Off, device: self.videoDeviceInput!.device)
                }

另一個簡短的方法是這樣做

        let devices = AVCaptureDevice.devices()
        let device = devices[0]
               guard device.isTorchAvailable else { return }
               do {
                 try device.lockForConfiguration()
                 if device.torchMode == .on {
                    device.torchMode = .off
                 }else{
                   device.torchMode = .on
                   }
               } catch {
                 debugPrint(error)
    }

Swift 5.4 & Xcode 12.4 & iOS 14.4.2


@objc private func flashEnableButtonAction() {

        guard let captureDevice = AVCaptureDevice.default(for: AVMediaType.video) else {
            return
        }
        
        if captureDevice.hasTorch {
            do {
                let _: () = try captureDevice.lockForConfiguration()
            } catch {
                print("aaaa")
            }
            
            if captureDevice.isTorchActive {
                captureDevice.torchMode = AVCaptureDevice.TorchMode.off
            } else {
                
                do {
                    let _ = try captureDevice.setTorchModeOn(level: 1.0)
                } catch {
                    print("bbb")
                }
            }
            
            captureDevice.unlockForConfiguration()
        }
        
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM