ios 读取 iPhone 的环境光传感器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6309643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading the iPhone's Ambient Light sensor
提问by George Johnston
I notice on my iPhone, after a few seconds of being in direct sun light, the screen will adjust to become brighter, dimmer etc. I was wondering if there was a way to interact with this sensor?
我注意到在我的 iPhone 上,在阳光直射下几秒钟后,屏幕会调整为更亮、更暗等。我想知道是否有办法与这个传感器交互?
I have an application which is used outside. When you go into direct light, it becomes very difficult to see the screen for a few momments, before it adjusts. And even then, it's not always as bright as I'd like it to be. I would like to implement a high contrast skin for outdoor viewing, and a low contrast for indoor viewing.
我有一个在外面使用的应用程序。当您进入直射光下时,在屏幕调整之前的片刻内很难看到屏幕。即便如此,它也不总是像我希望的那样明亮。我想为户外观看实现高对比度皮肤,为室内观看实现低对比度。
Is this possible to read light sensor data, and if so, how do I extract these sensor values?
这是否可以读取光传感器数据,如果可以,我如何提取这些传感器值?
I would assume there is a light sensor however, as the camera knows when to use the flash.
然而,我假设有一个光传感器,因为相机知道何时使用闪光灯。
采纳答案by PengOne
Try using GSEventSetBacklightLevel();
, which requires <GraphicsServices/GraphicsServices.h>
. This is how one can programmatically adjust the brightness levels. There is also a get
option, so I think that may have the information you're after.
尝试使用GSEventSetBacklightLevel();
,这需要<GraphicsServices/GraphicsServices.h>
. 这就是如何以编程方式调整亮度级别。还有一个get
选项,所以我认为这可能包含您想要的信息。
回答by Francisco Gutiérrez
On the other hand this is a different idea (maybe a silly one), using the brightness of the device's screen you can get some value of the external conditions.
另一方面,这是一个不同的想法(可能是一个愚蠢的想法),使用设备屏幕的亮度,您可以获得外部条件的一些值。
From 0.12 (Dark) to 0.99 (Light)
从 0.12(暗)到 0.99(亮)
The next line will get those values, give it a try, put some light on and off over the device to get different values.
下一行将获取这些值,尝试一下,在设备上打开和关闭一些灯以获得不同的值。
NSLog(@"Screen Brightness: %f",[[UIScreen mainScreen] brightness]);
Obviously Automatic Brightness feature should be turned on in order to get this to work.
显然,应该打开自动亮度功能才能使其正常工作。
Regards.
问候。
回答by Robert
To read the ambient light sensor data, you need to use IOHID in the IOKit framework.
要读取环境光传感器数据,您需要使用 IOKit 框架中的 IOHID。
http://iphonedevwiki.net/index.php/AppleISL29003
http://iphonedevwiki.net/index.php/AppleISL29003
http://iphonedevwiki.net/index.php/IOKit.framework
http://iphonedevwiki.net/index.php/IOKit.framework
However, this requires private headers, so if you use it, Apple probably won't let your app into the app store.
但是,这需要私有标头,因此如果您使用它,Apple 可能不会让您的应用进入应用商店。
I continually ask the iOS forums whether there will be support for ambient light sensor readings in the future, but to no avail.
我不断地询问 iOS 论坛将来是否会支持环境光传感器读数,但无济于事。
回答by Wildaker
You can actually use the camera to do this, which is independent of the user's screen brightness settings (and works even if Automatic Brightness is OFF).
您实际上可以使用相机来执行此操作,这与用户的屏幕亮度设置无关(即使自动亮度关闭也能正常工作)。
You read the Brightness Value from the video frames' metadata as I explain in this Stack Overflow answer.
正如我在Stack Overflow answer 中解释的那样,您从视频帧的元数据中读取亮度值。
回答by Stéphane de Luca
For Swift 5, here is how to use the brightness detection which indirectly gives you the luminosity of the outside:
对于 Swift 5,以下是如何使用亮度检测间接为您提供外部亮度:
/// A view controller (you can use any UIView or AnyObj)
class MyViewConroller: UIViewController {
/// Remove observers on deinit
deinit {
removeObservers()
}
// MARK: - Observers management helpers
/// Add my observers to the vc
func addObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(onScreenBrightnessChanged(_:)), name: UIScreen.brightnessDidChangeNotification, object:nil)
}
/// Clean up observers
func removeObservers() {
NotificationCenter.default.removeObserver(self)
}
/// Load the views
func loadView() {
// Add my observes to the vc
addObservers()
}
/**
Handles brightness changes
*/
@objc func onScreenBrightnessChanged(_ sender: Notification) {
// Tweak as needed: 0.5 is a good value for me
let isDark = UIScreen.main.brightness < 0.5. // in 0...1
// Do whatever you want with the `isDark` flag: here I turn the headlights off
vehicle.turnOnTheHeadlights( isDark )
}
}