IOS开发之截图(snapshot picture)

最近要实现一个截图分享的功能,开始是丈二和尚摸不着头脑,后来一查还挺简单的。

写个函数就搞定:

- (UIImage *) captureScreen { UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow]; CGRect rect = [keyWindow bounds]; UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); [keyWindow.layer renderInContext:context]; UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; }

如果要保存到相册:

- (void)saveScreenshotToPhotosAlbum:(UIView *)view { UIImageWriteToSavedPhotosAlbum([self captureScreen], nil, nil, nil); }

你可能感兴趣的:(ios,snapshot,Object-C,屏幕截图)