-(UIImage *)snapshotScreen{
UIView *view = [[UIApplication sharedApplication] keyWindow];
UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *image=[self snapshotScreen];
NSData *data=UIImagePNGRepresentation(image);
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
NSLog(@"image:%@ imageLength:%ld",image,data.length);
}
UIView *view = [[UIApplication sharedApplication] keyWindow];
UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//按钮事件(开始截屏)
UIImage *image=[self snapshotScreen];
NSData *data=UIImagePNGRepresentation(image);
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
NSLog(@"image:%@ imageLength:%ld",image,data.length);
}
本文介绍了一个简单的iOS应用中实现屏幕截图的方法。通过获取当前窗口并绘制到图像上下文中,可以将屏幕内容保存为UIImage,并进一步保存到相册中。
6645

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



