ios 有没有办法以编程方式调整屏幕亮度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3779667/
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
Is there a way of adjusting the screen brightness programmatically?
提问by openfrog
I have an iPhone app for photographic purposes (kind of a lightbox). This app needs as much brightness as possible. Is there a way to change the screen brightness programmatically, and then restore it back some time later?
我有一个用于摄影的 iPhone 应用程序(有点像灯箱)。这个应用程序需要尽可能多的亮度。有没有办法以编程方式更改屏幕亮度,然后在一段时间后将其恢复?
回答by Alastair Stuart
Edit: iOS 5 now includes a screen brightness API.
编辑:iOS 5 现在包含一个屏幕亮度 API。
[[UIScreen mainScreen] setBrightness:0.5];
Previous answer:
上一个答案:
No, this capability is not exposed via public APIS.
不,此功能不会通过公共 APIS 公开。
Edit: Note that a future possible iOS release may or may not have screen brightness on the multitasking bar on one particular iDevice.
编辑:请注意,未来可能的 iOS 版本可能会或可能不会在一个特定 iDevice 上的多任务栏上显示屏幕亮度。
回答by Emil
It is possible, but your app will most likely get rejected from the App Store, because it uses a private API. A flashlight-app was rejected because it adjusted the screen brightness, so I wouldn't recommend it.
这是可能的,但您的应用很可能会被 App Store 拒绝,因为它使用私有 API。手电筒应用程序被拒绝,因为它调整了屏幕亮度,所以我不推荐它。
In iOS 4.2, the iPad will have a screen brightness-adjuster in the multitasking-bar,
在 iOS 4.2 中,iPad 的多任务栏中会有一个屏幕亮度调节器,
(as mentioned by @coob) so you could tell your users to set the brightness there.
(正如@coob 提到的)所以你可以告诉你的用户在那里设置亮度。
What you could do, is to create a black overlay-view, and set it's alpha to more or less, according to how bright you want the screen to be. This won't actually make the screen less/more bright, but it will give the user an illusion of that.
你可以做什么,是创建一个黑色的叠加图,它的alpha设置或多或少,根据你想如何亮屏幕是。这实际上不会使屏幕变暗/变亮,但会给用户一种错觉。
回答by Glenn Howes
Here's a Swift answer to this question.
这是对这个问题的 Swift 答案。
import UIKit
extension UIScreen
{
static func setMainBrightness(brightness: CGFloat)
{
guard (0...1).contains(brightness) else
{
print("Attempt to set the screen brightness to an invalid value: \(brightness) should be between 0 and 1 inclusive.")
return
}
self.main.brightness = brightness
}
}
Call it by using:
使用以下方法调用它:
UIScreen.setMainBrightness(0.5)
Or ignore my extension (which I just wrote to illustrate the limits) and just call:
或者忽略我的扩展(我只是为了说明限制而写的)并调用:
UIScreen.main.brightness = 0.5