本文介绍了如何以编程方式截取 iPhone 的屏幕截图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在目标 C 中,我们是否可以拍摄屏幕的屏幕截图并将此图像存储在 UIImage 中.
Is it possible in objective C that we can take the screen shot of screen and stored this image in UIImage.
推荐答案你需要创建一个你的屏幕大小的位图上下文并使用
You need to create a bitmap context of the size of your screen and use
[self.view.layer renderInContext:c]在其中复制您的视图.完成后,您可以使用
to copy your view in it. Once this is done, you can use
CGBitmapContextCreateImage(c)根据您的上下文创建一个 CGImage.
to create a CGImage from your context.
细化:
CGSize screenSize = [[UIScreen mainScreen] applicationFrame].size; CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGContextRef ctx = CGBitmapContextCreate(nil, screenSize.width, screenSize.height, 8, 4*(int)screenSize.width, colorSpaceRef, kCGImageAlphaPremultipliedLast); CGContextTranslateCTM(ctx, 0.0, screenSize.height); CGContextScaleCTM(ctx, 1.0, -1.0); [(CALayer*)self.view.layer renderInContext:ctx]; CGImageRef cgImage = CGBitmapContextCreateImage(ctx); UIImage *image = [UIImage imageWithCGImage:cgImage]; CGImageRelease(cgImage); CGContextRelease(ctx); [UIImageJPEGRepresentation(image, 1.0) writeToFile:@"screen.jpg" atomically:NO];请注意,如果您运行代码以响应对 UIButton 的点击,您的图像将显示该按钮已按下.
Note that if you run your code in response to a click on a UIButton, your image will shows that button pressed.
更多推荐
如何以编程方式截取 iPhone 的屏幕截图?
发布评论