Two problems with current solutions:
placing views over other views can (in my experience with UICollectionView and other crap) trigger autorun. Auto power off is bad. Autolayout causes the machine to spike and may have side effects, other than just computing without any reason, other than burning the processor and battery. Thus, you do not want to indicate the reasons for auto-shutdown, for example, adding a subtitle to a view that really really cares about keeping itself in a good location.
your view does not necessarily cover the entire screen, so if you want to burn the whole screen, you better use UIWindow ... this should isolate from any view controllers that are touchy added subviews
This is my implementation, category in UIView. I have included methods to take a screenshot before the window blinks, and then save it to the camera frame. Note that UIWindow seems to want to make its own animations when added, will usually disappear in more than a third of a second. Maybe the best way to tell him not to do this.
// stupid blocks typedef void (^QCompletion)(BOOL complete); @interface UIView (QViewAnimation) + (UIImage *)screenshot; // whole screen + (void)flashScreen:(QCompletion)complete; - (UIImage *)snapshot; // this view only - (void)takeScreenshotAndFlashScreen; @end @implementation UIView (QViewAnimation) + (UIImage *)screenshot; { NSArray *windows = [[UIApplication sharedApplication] windows]; UIWindow *window = nil; if (windows.count) { window = windows[0]; return [window snapshot]; } else { NSLog(@"Screenshot failed."); return nil; } } - (UIImage *)snapshot; { UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0); [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } + (void)flashScreen:(QCompletion)complete; { UIScreen *screen = [UIScreen mainScreen]; CGRect bounds = screen.bounds; UIWindow * flash = [[UIWindow alloc] initWithFrame:bounds]; flash.alpha = 1; flash.backgroundColor = [UIColor whiteColor]; [UIView setAnimationsEnabled:NO]; [flash makeKeyAndVisible]; [UIView setAnimationsEnabled:YES]; [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationCurveEaseOut animations:^{ flash.alpha = 0; } completion: ^(BOOL finished) { flash.hidden = YES; [flash autorelease]; if (complete) { complete(YES); } }]; } - (void)takeScreenshotAndFlashScreen; { UIImage *image = [UIView screenshot]; [UIView flashScreen:^(BOOL complete){ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^{ UIImageWriteToSavedPhotosAlbum(image,self,@selector(imageDidFinishSaving:withError:context:),nil); }); }]; } - (void)imageDidFinishSaving:(UIImage *)image withError:(NSError *)error context:(void *)context; { dispatch_async(dispatch_get_main_queue(),^{ // send an alert that the image saved }); } @end
nobody
source share