Created
May 20, 2014 12:18
-
-
Save zats/1a4aece697075478b44a to your computer and use it in GitHub Desktop.
Fix for ImagePicker's camera resetting [UIApplication sharedApplication].idleTimerDisabled = YES;
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface ZTSCameraControllerHelper () | |
@property (nonatomic, assign, getter = isIdleTimerDisabled) BOOL idleTimerDisabled; | |
@end | |
@implementation ZTSCameraControllerHelper | |
+ (instancetype)sharedInstance { | |
static ZTSCameraControllerHelper *instance; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
instance = [[ZTSCameraControllerHelper alloc] init]; | |
}); | |
return instance; | |
} | |
+ (void)load { | |
[self swizzleMethods]; | |
} | |
+ (void)swizzleMethods { | |
// To make sure PhotoLibrary framework classes are registered | |
UIImagePickerController *picker = [[UIImagePickerController alloc] init]; | |
picker = nil; | |
Class cameraControllerClass = NSClassFromString(@"PLCameraController"); | |
// method called just before we render camera preview for the first time | |
Method delayMethod = class_getInstanceMethod(self, @selector(zts_delayIdleTimerByTimeInterval:)); | |
IMP delayIMP = method_getImplementation(delayMethod); | |
PSPDFReplaceMethod(cameraControllerClass, sel_getUid("_delayIdleTimerByTimeInterval:"), @selector(zts_delayIdleTimerByTimeInterval:), delayIMP); | |
// method called after camera preview stopped | |
Method resetIdleTimerMethod = class_getInstanceMethod(self, @selector(zts_resetIdleTimer)); | |
IMP resetIdleTimerIMP = method_getImplementation(resetIdleTimerMethod); | |
PSPDFReplaceMethod(cameraControllerClass, sel_getUid("_resetIdleTimer"), @selector(zts_resetIdleTimer), resetIdleTimerIMP); | |
} | |
- (void)zts_delayIdleTimerByTimeInterval:(NSTimeInterval)delay { | |
[ZTSCameraControllerHelper sharedInstance].idleTimerDisabled = [UIApplication sharedApplication].isIdleTimerDisabled; | |
if (![ZTSCameraControllerHelper sharedInstance].isIdleTimerDisabled) { | |
[self zts_delayIdleTimerByTimeInterval:delay]; | |
} | |
} | |
- (void)zts_resetIdleTimer { | |
if (![ZTSCameraControllerHelper sharedInstance].isIdleTimerDisabled) { | |
[self zts_resetIdleTimer]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fix for ImagePicker's camera resetting
[UIApplication sharedApplication].idleTimerDisabled = YES;
The same issue happens when you're using dictation feature - it effectively resets the
idleTimerDisabled
toNO
.The faulty calls are
-[UIDictationController setupForDictationStartForReason:]
and-[UIDictationController cancelDictation]