在调用相机后idleTimerDisabled失效的问题
在调用相机后idleTimerDisabled失效的问题
相关资料:
问题
- 前几天有人在群里边说,设置idleTimerDisabled=YES后,在使用相机后不会阻止锁屏(很好奇怎么回事)。
解决
1、完全不了解情况,先试了一下(代码来源):
- (void)viewDidLoad
{
[super viewDidLoad]; self.button = [UIButton buttonWithType:UIButtonTypeSystem];
[self.button setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.button setTitle:@"Show ImagePicker" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(buttonTouchHandler:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
[self.view setNeedsUpdateConstraints];
} - (void)buttonTouchHandler:(UIButton *)button
{
[self logIdleTimerDisabled];
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];
} - (void)updateViewConstraints
{
[super updateViewConstraints]; if (self.viewConstraints)
{
[self.view removeConstraints:self.viewConstraints];
} NSMutableDictionary *views = [@{ @"button": self.button} mutableCopy];
NSMutableArray *constraints = [NSMutableArray array];
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[button]-20-|" options:0 metrics:nil views:views]];
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[button]-|" options:0 metrics:nil views:views]]; self.viewConstraints = [constraints copy];
[self.view addConstraints:self.viewConstraints];
} - (void)logIdleTimerDisabled
{
BOOL idleTimerDisabled = [UIApplication sharedApplication].idleTimerDisabled;
NSLog(@"idleTimerDisabled = %d", idleTimerDisabled);
} #pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self logIdleTimerDisabled];
[self dismissViewControllerAnimated:YES completion:^{
[self logIdleTimerDisabled];
}];
} - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self logIdleTimerDisabled];
[self dismissViewControllerAnimated:YES completion:^{
[self logIdleTimerDisabled];
}];
}
注:Appdelegate中已近设置idleTimerDisabled为YES,Xcode会阻止设备休眠。
Log:2017-03-23 13:22:25.594 UIImagePickerControllerBug[30239:3267578] idleTimerDisabled = 1
2017-03-23 13:22:28.476 UIImagePickerControllerBug[30239:3267578] Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.
2017-03-23 13:22:32.312 UIImagePickerControllerBug[30239:3267578] idleTimerDisabled = 1
2017-03-23 13:22:32.846 UIImagePickerControllerBug[30239:3267578] idleTimerDisabled = 0
2、上边的『代码来源』原文中有作者的一些猜测:
I have attached a simplified Xcode project to demonstrate the bug. 1. Run UIImagePickerControllerBug on a device not connected to Xcode (because Xcode always keeps the device from sleeping).
2. Observe that the app does not fall asleep.
3. Press the Show ImagePicker Button.
4. Take a photo or just press cancel
5. Observe that the app will now fall asleep.
3、在『stackoverflow.com』查找的时候,发现其中有人在很早以前就提出了这个问题(由于是前些天的事情有些链接找不到了),不知道是出于什么原因官方一直没有解决。
解决
解决办法就是相机的完成或者取消代理中延时设置,详见:
- (void)resetIdleTimerDisabled
{
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
} #pragma mark - UIImagePickerControllerDelegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:^{
[self performSelector:@selector(resetIdleTimerDisabled) withObject:nil afterDelay:1.0];
}];
} - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:^{
[self performSelector:@selector(resetIdleTimerDisabled) withObject:nil afterDelay:1.0];
}];
}
备注
- Xcode会阻止设备休眠。
在调用相机后idleTimerDisabled失效的问题的更多相关文章
- ios 调用相机后 view 下沉问题
我只加了一句代码 现在不报错了 因为这个问题是随机性的 我也不太明白这个地方是怎么回事 我只是这样子做了 问题不出来了 if ([[[UIDevice currentDevice] syst ...
- swift调用相机和相册
简单实现swift调用相机和相册的功能,分享代码与学习swift的童鞋共同进步 import UIKit class ViewController: UIViewController,UIImageP ...
- iOS-iOS调用相机调用相册【将图片保存到本地相册】
设置头部代理 <UINavigationControllerDelegate, UIImagePickerControllerDelegate> 1.调用相机 检测前置摄像头是否可用 - ...
- Android调用相机拍摄照片并显示到 ImageView控件中
在前面的一篇文章中曾介绍过简单的开启相机照相功能,详见 Android简单调用相机Camera功能,实现打开照相功能 ,这一次就会将前面拍摄的照片显示到ImageView中,形成一个完整的效果 看实例 ...
- Android 调用相机、相册功能
清单文件中增加对应权限,动态申请权限(此部分请参考Android 动态申请权限,在此不作为重点描述) private static final int REQUEST_CODE_ALBUM = 100 ...
- Android开发在Activity外申请权限调用相机打开相册
问题描述: 最近在项目中遇到一个需要调用相册和打开相机的需求,但是,在Android 6.0以后,调用相册属于危险权限,需要开发者动态获取,这就意味着我们申请权限是与Activity绑定的,但如果一个 ...
- 【转】Expire Google Drive Files 让Google Docs云盘共享连接在指定时间后自动失效
最近在清理Google Docs中之前共享过的文件链接,发现Google Docs多人协作共享过的链接会一直存在,在实际操作中较不灵活.正好订阅的RSS推送了Pseric写的这篇文章 - Expire ...
- Xcode升后插件失效
Xcode升后插件失效,与添加插件不小心点击Skip Bundle解决办法 字数267 阅读4731 评论1 喜欢12 今天升级了xcode到6.4 发现之前装的插件不能使用了.这里有一个解决的方案: ...
- 更新xcode后插件失效问题——不针对特定版本的通用解决方法
一.Xcode更新后插件失效的原理 1.每次更新Xcode后插件都会失效,其实插件都还在这个目录好好的躺着呢: ~/Library/Application Support/Developer/Shar ...
随机推荐
- GIS制图课程目录
由于更新次序跳跃式更新,因此很有必要整理一下全书目录,并将会按照实际学习的顺序进行更新. [前言] GIS制图课程前言 [理论篇] 理论篇-地图学与GIS制图的基础理论(一) 理论篇-地图学与GIS制 ...
- UI进阶 XML解析适配 引入GDataXML文件时候 'libxml/tree.h'file not found 错误解决办法
在工程的"Build Settings"页中找到"Header Search Path"项,添加"/usr/include/libxml2" ...
- IP分类以及特殊IP
一.IP分类 点分十进制数表示的IPv4 地址分成几类,以适应大型.中型.小型的网络.这些类的不同之处在于用于表示网络的位数与用于表示主机的位数之间的差别.IP地址分成五类,用字母表示: ...
- java继承(一)
虽然说java中的面向对象的概念不多,但是具体的细节还是值得大家学习研究,java中的继承实际上就是子类拥有父类所有的内容(除私有信息外),并对其进行扩展.下面是我的笔记,主要包含以下一些内容点: 构 ...
- Python简单爬虫记录
为了避免自己忘了Python的爬虫相关知识和流程,下面简单的记录一下爬虫的基本要求和编程问题!! 简单了解了一下,爬虫的方法很多,我简单的使用了已经做好的库requests来获取网页信息和Beauti ...
- 关于OpenGL和DX学习的取舍
大家多知道左右就肯定要与显卡打交道.两大图形图像IPA.OpenGL(图形),DX(图形,声音,键盘控制,网络) OpenGL的兴起可能取决于苹果公司的适用,吸引看大部分开发者适用,它有跨平台的有点. ...
- oracle系列笔记(1)---查询数据
查询数据 1. 查询(select .. form ..) (1)普通查询 select * from employees --代表查询employees表中所有数据 select last_n ...
- Mac上写C++
用惯Windows的同学可能刚开始用Mac的时候并不知道如何写C++,我刚开始在Mac上写C++的时候也遇到过这个困扰,Mac上并没有Windows上自己用习惯的Visual C++,下面我分享一下个 ...
- HTTP严格安全传输(HTTP Strict Transport Security, HSTS)chromuim实现源码分析(二)
HTTP严格安全传输(HTTP Strict Transport Security, HSTS)chromuim实现源码分析(一) 下面来查看其他对保存HSTS信息的enabled_sts_hosts ...
- rsyslog+mariadb+loganalyzer实现日志服务器搭建
rsyslog+mariadb+loganalyzer实现日志服务器搭建 一.概述 Linux的日志记录了用户在系统上一切操作,包括系统自身运作产生的日志,这些日志是应使用者了解服务器的情况最好的资料 ...