如何模拟在iOS模拟器中截取屏幕截图?

Edw*_*ehr 15 ios ios-simulator

有没有办法模拟在iOS模拟器上拍摄屏幕截图(相当于家用+设备上电)?我的目标不是保存(cmd + s)或复制屏幕截图(来自模拟器菜单项),而是捕获UIApplicationUserDidTakeScreenshotNotification事件.

Ala*_*ino 11

使用LLDB模拟屏幕截图NSNotification:

(lldb) expr [[NSNotificationCenter defaultCenter] postNotificationName:(NSNotificationName)UIApplicationUserDidTakeScreenshotNotification object:nil]
Run Code Online (Sandbox Code Playgroud)

如果要在断点处停止,则传递--ignore-breakpoints false --expr命令:

(lldb) expr --ignore-breakpoints false -- [[NSNotificationCenter defaultCenter] postNotificationName:(NSNotificationName)UIApplicationUserDidTakeScreenshotNotification object:nil]
Run Code Online (Sandbox Code Playgroud)


nis*_*sar 0

我也无法检测到 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();
Run Code Online (Sandbox Code Playgroud)