[英]How to simulate taking a screenshot in iOS simulator?
有没有办法模拟在iOS模拟器上拍摄屏幕截图(相当于家用+设备上电)? 我的目标不是保存(cmd + s)或复制屏幕截图(来自模拟器菜单项),而是捕获UIApplicationUserDidTakeScreenshotNotification事件。
使用LLDB模拟屏幕截图NSNotification:
(lldb) expr [[NSNotificationCenter defaultCenter] postNotificationName:(NSNotificationName)UIApplicationUserDidTakeScreenshotNotification object:nil]
如果要在断点处停止,则将--ignore-breakpoints false --
传递给expr
命令:
(lldb) expr --ignore-breakpoints false -- [[NSNotificationCenter defaultCenter] postNotificationName:(NSNotificationName)UIApplicationUserDidTakeScreenshotNotification object:nil]
不,这在iOS模拟器中目前无法实现。
我也无法检测UIApplicationUserDidTakeScreenshotNotification,但为什么不使用此代码来拍摄屏幕截图并使用条件检测它们。
// // TAKE SCREENSHOT
CGRect myRect = [self.view bounds];
UIGraphicsBeginImageContext(myRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, myRect);
[self.view.layer renderInContext:ctx];
UIImage *viewimage = UIGraphicsGetImageFromCurrentImageContext();
NSData *imageData = UIImageJPEGRepresentation(viewimage, 1.0);
if(imageData!=NULL)
{
NSLog(@"user saved the image");
//Here you can detect the screen shots
[self.imagevieww setImage:[UIImage imageWithData:imageData]];
}
else
{
NSLog(@"user dont want to save image");
}
UIGraphicsEndImageContext();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.