在调用相机后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失效的问题的更多相关文章

  1. ios 调用相机后 view 下沉问题

    我只加了一句代码 现在不报错了  因为这个问题是随机性的  我也不太明白这个地方是怎么回事   我只是这样子做了  问题不出来了 if ([[[UIDevice currentDevice] syst ...

  2. swift调用相机和相册

    简单实现swift调用相机和相册的功能,分享代码与学习swift的童鞋共同进步 import UIKit class ViewController: UIViewController,UIImageP ...

  3. iOS-iOS调用相机调用相册【将图片保存到本地相册】

    设置头部代理 <UINavigationControllerDelegate, UIImagePickerControllerDelegate> 1.调用相机 检测前置摄像头是否可用 - ...

  4. Android调用相机拍摄照片并显示到 ImageView控件中

    在前面的一篇文章中曾介绍过简单的开启相机照相功能,详见 Android简单调用相机Camera功能,实现打开照相功能 ,这一次就会将前面拍摄的照片显示到ImageView中,形成一个完整的效果 看实例 ...

  5. Android 调用相机、相册功能

    清单文件中增加对应权限,动态申请权限(此部分请参考Android 动态申请权限,在此不作为重点描述) private static final int REQUEST_CODE_ALBUM = 100 ...

  6. Android开发在Activity外申请权限调用相机打开相册

    问题描述: 最近在项目中遇到一个需要调用相册和打开相机的需求,但是,在Android 6.0以后,调用相册属于危险权限,需要开发者动态获取,这就意味着我们申请权限是与Activity绑定的,但如果一个 ...

  7. 【转】Expire Google Drive Files 让Google Docs云盘共享连接在指定时间后自动失效

    最近在清理Google Docs中之前共享过的文件链接,发现Google Docs多人协作共享过的链接会一直存在,在实际操作中较不灵活.正好订阅的RSS推送了Pseric写的这篇文章 - Expire ...

  8. Xcode升后插件失效

    Xcode升后插件失效,与添加插件不小心点击Skip Bundle解决办法 字数267 阅读4731 评论1 喜欢12 今天升级了xcode到6.4 发现之前装的插件不能使用了.这里有一个解决的方案: ...

  9. 更新xcode后插件失效问题——不针对特定版本的通用解决方法

    一.Xcode更新后插件失效的原理 1.每次更新Xcode后插件都会失效,其实插件都还在这个目录好好的躺着呢: ~/Library/Application Support/Developer/Shar ...

随机推荐

  1. TSQL编程

    1.索引 唯一键/主键添加索引,设计界面,在任何一列前右键--索引/键--点击进入添加某一列为索引 2.视图 视图就是我们查询出来的虚拟表创建视图:create view 视图名  as  SQL查询 ...

  2. C++编程练习(9)----“图的存储结构以及图的遍历“(邻接矩阵、深度优先遍历、广度优先遍历)

    图的存储结构 1)邻接矩阵 用两个数组来表示图,一个一维数组存储图中顶点信息,一个二维数组(邻接矩阵)存储图中边或弧的信息. 2)邻接表 3)十字链表 4)邻接多重表 5)边集数组 本文只用代码实现用 ...

  3. Python自然语言处理学习笔记之信息提取步骤&分块(chunking)

    一.信息提取模型 信息提取的步骤共分为五步,原始数据为未经处理的字符串, 第一步:分句,用nltk.sent_tokenize(text)实现,得到一个list of strings 第二步:分词,[ ...

  4. 理解javascript this 的绑定过程

    在理解this 的绑定过程之前,首先要理解调用位置:调用位置就是函数在代码中被调用的位置(而不是声明的位置).只有仔细分析调用位置才能回答这个问题:这个this 到底引用的是什么?通常来说,寻找调用位 ...

  5. LINQ查询表达式基础

    LINQ,语言集成查询(Language Integrated Query)是一组用C#和Visual Basic语言的扩展. 对于编写查询的开发人员来说,LINQ 最明显的"语言集成&qu ...

  6. api接口json串换行

    1.问题描述:在后台输入框中明明回车换行了,但是返回到 app客户端显示出来的 确实带有 \n  这个时候无论怎么调试都不行: 2.铺垫:大家都知道 php输出字符串的时候  使用 单引号 比使用 双 ...

  7. bootstrap模态框总结

    <html lang="zh-cn"> <head> <meta charset="utf-8"> <title> ...

  8. Android apk应用程序签名

    Android apk应用程序签名 分类: Android 2012-11-25 19:33 570人阅读 评论(0) 收藏 举报 一.Android Apk签名 Apk签名首先要有一个keystor ...

  9. 2017年2月22日-----------乱码新手自学.net 之Entity Framework 增删改

    由于我是自学的,没有人教,在网上查资料也查不到个所以然.问大神们也是爱理不理的. 所以这篇随笔纯粹源自于我自己的认识.是否真正正确我也没有把握. 如果有什么错误,请大神们给予指正 ========== ...

  10. python2.7学习笔记-split用一个分隔符分割、多个分隔符分割、在有汉字存在的情况下split分割

    还是直接上程序吧,暂且就叫它为程序吧: ''' 按照多个分隔符分割,split的用法 ''' import re s='hi!chu?lan!heihei' re.split('[!?]',s) '' ...