How would I program the ability to change brightness in-app? I know that its possible as I have seen at least three apps that can do it. This would be very useful for my app. I know that it's only possible in iOS 5 with the UIScreen Class, but I have no idea how to program it. Please help me!
The UIScreen class has a new property called brightness.
In addition, there's another property called wantsSoftwareDimming that (when set to YES) allows you to go below the lowest brightness supported by the hardware, because a special "dimming view" is overlaid over the screen to darken things even further.
The brightness property takes a float from 0 to 1. So:
with wantsSoftwareDimming set to NO (the default), a brightness of 0 means "the darkest the hardware supports" and a brightness of 1 means "the brightest the hardware supports".
with wantsSoftwareDimming set to YES, a brightness of 0 means "the darkest the hardware supports PLUS darkening by overlaying a dimming view", and a brightness of 1 still means "the brightest the hardware supports".
I have never tried it, but looking at the docs it should go like this:
UIScreen *mainScreen = [UIScreen mainScreen];
mainScreen.brightness = .5; //should set the brightness at 50%
As others pointed out you can use
[[UIScreen mainScreen] setBrightness:1.0];
BUT be very careful because you will run into problems (talking from experience here)
look at this:
IOS5 setBrightness didn't work with applicationWillResignActive
and this:
Anyone been able to use [[UIScreen mainScreen] setBrightness] on background / exit?
(I wish I had, when I discovered this thread/answer) :-(
You can use either of these two:
[[UIScreen mainScreen]setBrightness:1.0];
GSEventSetBacklightLevel(0.5f); But this is a private API call, if you use it, your application will surely be rejected.
Related
I am trying to turn off device screen when orientation is isFaceDown, the way to turn off screen as google results suggest is by using following code
[UIDevice currentDevice].proximityMonitoringEnabled = YES;
or
[[UIScreen mainScreen] setBrightness:0.0];
the last one doesn't serves my purpose as its still not completely turned off , where as proximity sensor doesnot seems to work always when I face down my device on my desk, so the question is does it sense proximity to every object, cause from my experience thats not always true or am i missing something here. Also could someone suggest a way to achieve turning off screen completely when orientation of the device is face down?
As #Pétur stated in his answer, the proximity sensory was designed to detect skin in close proximity, but that isn't your biggest issue here.
[[UIScreen mainScreen] setBrightness:0.0];
Is actually as close as you can get to turning the screen off. There is no public API that gives us this kind of power. The best you're going to get is to turn the brightness down to 0.0, hide the status bar, and create a all black view to present over top of the rest of your app.
The proximity sensor is an infra-red sensor that detects heat radiation. You can use it to detect a face but not dead items unless they are hot.
I have a next problem. I was searching in Google and here but still not found correct and proper solution. I have application which has to run on iPhone 4 and on iPhone 5. As we know iPhone 5 has a different screen size then 4th one. Let's see simple case, I have a view controller with background image. Image is size sensitive and if I will stretch it it will look ugly. So I have two different images one image for old 4th resolution and and another image for new 5th one. The question is what is the best and let's say "native" way to implement correct image showing on the both devices.
I see simplest way to do it is making runtime checking which device currently used and set proper image name to UIImageView. But I find it ugly. Does somebody know the correct way to do it?
You can use:
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)
Then,
if (IS_IPHONE5) {
//iPhone5
} else {
/older devices
}
Source: How to get the actual [UIScreen mainScreen] frame size?
I try to display an app in external display, actually, it is a giant Iphone monitor (height is 1.5 m and wild is .5m ) and running into an issue. It is that my app is displayed in landscape mode with height is 320px and wild is 460px. However, in the giant iphone, my app is half of screen. How can I make it fit 1.5m x 0.5 m. Are there any ways to trick an system so that I can display a full screen in gian Iphone monitor even though the height and width of actual iphone is 320 and 460 respectively.
Please help if you have any ideas. All answer are welcomed here. Thanks
Be sure you scale your output appropriately for the external display. Something like this should get you on your way:
// if an external screen is attached it will be in the array of screens
if ( [[UIScreen screens] count] > 1 )
{
UIScreen *screen2 = [[UIScreen screens] objectAtIndex:1];
CGRect screenBounds = screen2.bounds;
NSLog(#"Should scale external output to fit in %dx%d",
(int)screenBounds.size.width,
(int)screenBounds.size.height);
}
There's lots of good information in Apple's View Programming Guide for iOS and the reference for UIScreen.
I guess it's worth mentioning that you are very unlikely to get any UI interaction (touch events, etc) from an external display. Most displays aren't touch displays and I'm don't believe touch events are even supported on an external display. So, any configuration of the information displayed on the external display will need to be controlled from the iOS device.
As a new AppDeveloper, I've never used orientation. So, I would like to try to learn with a simple task. What I want to do is have the screen fade to black when the device is set face down. Is that something that would be simple to do, that perhaps somebody could assist me in, or provide helpful information?
Thanks! :D
Your help is appriciated
You can use orientation (using the XYZ values when the screen is face down). I do not recommend this, because your screen will fade out even if a user is using the app while lying down, and staring up at the screen.
There is an easier and cleaner way. Notice how during phone calls, having the phone close to your ear blacks out the screen?
You can access that property by monitoring the proximityState property of UIDevice. Details here
Doing something like:
BOOL closeToUser = [[UIDevice currentDevice] proximityState];
will assign a YES to closeUser when the device is face down on a surface of some kind, and a NO when it is not
If the value is YES, you can invoke code to do whatever you want.
Update
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScreen_Class/index.html#//apple_ref/occ/instp/UIScreen/brightness
That's the Apple Doc for controlling screen brightness. Below is the original question.
I have found by using Google that I can disable the iPhone going to sleep in an application by using:
application.idleTimerDisabled = YES;
so the code looks like this:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
// This disables the autosleep I added this to TEST,
// delete later if you want:-
application.idleTimerDisabled = YES;
[window addSubview:switchViewController.view];
[window makeKeyAndVisible];
Perfect, it works well. However, my question is, can I somehow disable the iPhone from going to sleep, while still allowing the screen to dim? Or perhaps dim the screen myself in the app as to save battery power?
I definitely don't want the iPhone sleeping, but I'd also like to be user friendly/battery friendly and dim the screen. (You know, like how you can set the iPhone to dim the screen X seconds before it goes to sleep.) Is there an effective way to do this?
I'm pretty sure there's nothing in the official SDK.
[(id)[UIApplication sharedApplication] setBacklightLevel:0.3f]; // or whatever value
works, but is of course undocumented. The recent experience with UIGetScreenImage indicates that perhaps the right strategy with useful but undocumented APIs is to use them and see what happens.
Failing that, has anybody ever measured if the phone's power consumption goes down if it's showing a black image, or does it not help unless you can turn down the backlight?
I use a "nightstand" type alarm clock app that, if you double-tap its clock screen, dims the screen by some amount.
I believe what it's probably really doing is laying a partially-opaque black UIView over its entire content. I think the backlight isn't really dimmed, but the black color laid over the screen makes it seem dimmer. It works.
Can you set proximityState to trick the iPhone into thinking that it is close to someone's ear? This would work with the iPhone, but not the iPod touch. There is no way to selectively turn adjust the brightness ... apps that dim the screen typically do so by putting a partly transparent image over the current one.
Starting with iOS 5 you can set UIScreen's brightness:
This property is only supported on the main screen. The value of this
property should be a number between 0.0 and 1.0, inclusive.
Brightness changes made by an app remain in effect only while the app
is active. The system restores the user-supplied brightness setting at
appropriate times when your app is not in the foreground. So if you
change the value of this property, you do not need to record the
previous value and restore it when your app moves to the background.
You can use this approach http://oleb.net/blog/2014/02/alarm-clock-apps-ios/
Just add a parameter into info.plist. And your app still run when your screen dim, your device locked.
When you debug, your app still run and never dim automatically, you just press lock button to test.
When you run your app without debug, you can see this feature do, like Music app of Apple, the music still play when device go to turn off screen.