- (UIImage *)rn_screenshot {
UIGraphicsBeginImageContext(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT));
if([self.view respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]){
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:NO];
}
else{
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = image.CGImage;
CGRect rect =CGRectMake(0, SCREEN_HEIGHT - 357, SCREEN_WIDTH, 357);//这里可以设置想要截图的区域
CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect];
NSData *imageData = UIImageJPEGRepresentation(sendImage, 0.75);
image = [UIImage imageWithData:imageData];
return image;
}
文章标题
iOS屏幕截图方法
最新推荐文章于 2024-04-22 16:59:44 发布
本文介绍了一种在iOS应用中实现屏幕截图的方法。该方法通过UIGraphicsBeginImageContext开始图像上下文,然后调用view的drawViewHierarchyInRect或layer的renderInContext方法来绘制视图层级,最终获取当前图像上下文中的图像并进行裁剪和压缩。
2611

被折叠的 条评论
为什么被折叠?



