iOS 屏幕截图
来源:互联网 发布:淘宝月销量怎么计算 编辑:程序博客网 时间:2024/04/28 07:08
/** *截图功能 */+(UIImage *)screenShotFromView:(UIView *)view area:(CGRect)rect dstSize:(CGSize)dsize { UIGraphicsBeginImageContextWithOptions(dsize, YES, 0); [[view layer] renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); CGImageRef imageRef = viewImage.CGImage; CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect); UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect]; NSData *imageViewData = UIImagePNGRepresentation(sendImage); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *pictureName= @"screenShow.png"; NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:pictureName]; //保存照片到沙盒目录 [imageViewData writeToFile:savedImagePath atomically:YES]; CGImageRelease(imageRefRect); //从手机本地加载图片 UIImage *bgImage2 = [[UIImage alloc]initWithContentsOfFile:savedImagePath]; return bgImage2;}- (void)screenShotFromScrollView:(UIScrollView *)view area:(CGRect)rect dstSize:(CGSize)dsize { UIImage* image = nil; UIGraphicsBeginImageContext(view.contentSize); { CGPoint savedContentOffset = view.contentOffset; CGRect savedFrame = view.frame; view.contentOffset = CGPointZero; view.frame = CGRectMake(0, 0, view.contentSize.width, view.contentSize.height); [view.layer renderInContext: UIGraphicsGetCurrentContext()]; image = UIGraphicsGetImageFromCurrentImageContext(); view.contentOffset = savedContentOffset; view.frame = savedFrame; } UIGraphicsEndImageContext(); if (image != nil) { NSLog(@"截图成功!"); } }
出现这个错,一般是因为 frame中的高或宽为0或负值。
<Error>: CGContextSaveGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
0 0
- IOS屏幕截图---普通截图
- IOS 屏幕截图
- iOS 屏幕截图
- iOS 截图 截取屏幕
- iOS 截图 截取屏幕
- iOS 屏幕截图
- iOS Quartz2D - 屏幕截图
- iOS 屏幕截图
- iOS屏幕截图
- IOS屏幕截图
- iOS截图整个屏幕
- iOS屏幕截图
- iOS屏幕截图功能
- iOS 屏幕截图
- ios 屏幕截图
- iOS屏幕截图事件
- iOS截取屏幕(屏幕截图)
- IOS中屏幕截图(区域截图)
- 链表倒数第k个节点
- redis持久化
- VS2008程序发布的环境配置
- 罗未:开源硬件的奇点
- vb.net连接远程服务器上的postgreSql
- iOS 屏幕截图
- 螺旋数阵(下)由内向外旋转的8种情况
- ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'
- 用AngularJS构建单页应用,根据需求加载JS文件的方法
- Android 中Parcelable的作用
- 自己亲自写的两本linux资料,免费下载,pdf文档
- prototype.js Ajax.Request使用小结
- swift头文件与变量getter和setter的学习见解
- Android 资源、素材文件的存放