UIImage вид не сохраняется при программной съемке скриншота - Objective-C - PullRequest
0 голосов
/ 18 декабря 2018

Я пытаюсь сделать снимок экрана с:

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();

Однако сохраненное изображение не включает изображение на снимке экрана (вместо этого отображается белый).Почему это?

Любая помощь приветствуется.

1 Ответ

0 голосов
/ 18 декабря 2018

Попробуйте это

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");
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...