how to take a screenshot of the iPhone programmatically?

前端 未结 11 1838
谎友^
谎友^ 2020-11-28 08:01

Is it possible in objective C that we can take the screen shot of screen and stored this image in UIImage.

11条回答
  •  無奈伤痛
    2020-11-28 08:28

    - (void)SnapShot {
        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
            UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
        } else {
            UIGraphicsBeginImageContext(self.view.bounds.size);
        }
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        NSData *data = UIImagePNGRepresentation(image);
        [data writeToFile:@"snapshot.png" options:NSDataWritingWithoutOverwriting error:Nil];
        [data writeToFile:@"snapshot.png" atomically:YES];
    
        UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:data], nil, nil, nil);
    }
    

提交回复
热议问题