在以编程方式进行屏幕截图时,uiimage并未保存



我正在尝试使用:

进行屏幕截图
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    UIGraphicsBeginImageContextWithOptions(self.contentView.bounds.size, NO, [UIScreen mainScreen].scale);
} else {
    UIGraphicsBeginImageContext(self.contentView.bounds.size);
}
[self.contentView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

但是,保存的图像不包含屏幕截图中的图像(改为显示白色)。为什么这是?

任何帮助。

尝试此

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    UIGraphicsBeginImageContextWithOptions(self.image.bounds.size, NO, 2.0);
} else {
    UIGraphicsBeginImageContext(self.image.bounds.size);
}
[self.image drawViewHierarchyInRect:self.image.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(image);
if (imageData) {
    [imageData writeToFile:@"screenshot.png" atomically:YES];
} else {
    NSLog(@"error while taking screenshot");
}

最新更新