renderInContext: 截屏

官方给的该方法的说明

/** Rendering properties and methods. **/

/* Renders the receiver and its sublayers into 'ctx'. This method

* renders directly from the layer tree. Renders in the coordinate space

* of the layer.

* WARNING: currently this method does not implement the full

* CoreAnimation composition model, use with caution. */

- (void)renderInContext:(CGContextRef)ctx;

渲染一个图层和其子图层到 'ctx' ; 注意上面的WARNING:   not implement the full CoreAnimation composition model

//renderInContext:使用此函数实现截图,在有核心动画效果的视图上有差错: not implement the full CoreAnimation composition model

- (UIImage*)screenShotFromView:(UIView*)mainView

{

CGFloatscale = [UIScreenmainScreen].scale;

CGSizeshotSize =CGSizeMake(mainView.bounds.size.width*scale, mainView.bounds.size.height*scale);

UIGraphicsBeginImageContextWithOptions(shotSize,YES,0);

//设置截屏大小

[mainView.layerrenderInContext:UIGraphicsGetCurrentContext()];

UIImage*viewImage =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

CGImageRefimageRef = viewImage.CGImage;

CGRectrect =CGRectMake(0,0, shotSize.width, shotSize.height);//这里可以设置想要截图的区域

CGImageRefimageRefRect =CGImageCreateWithImageInRect(imageRef, rect);

UIImage*image = [[UIImagealloc]initWithCGImage:imageRefRect];

//UIGraphicsBeginImageContext(mainView.bounds.size);

//[mainView.layer renderInContext:UIGraphicsGetCurrentContext()];

//UIImage *image= UIGraphicsGetImageFromCurrentImageContext();

//

//UIGraphicsEndImageContext();

//显示截图

//UIImageView *imaView = [[UIImageView alloc] initWithImage:image];

//imaView.frame = CGRectMake(0, 0, mainView.frame.size.width/2, mainView.frame.size.height/2);

//[[UIApplication sharedApplication].keyWindow addSubview:imaView];

//写入相册

//UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);

returnimage;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 数组和字典等集合类型,可以通过下标的方式来快速获取相对应的值。在swift中,可以通过subscript来实现这个...
    __Guan__阅读 2,915评论 1 1
  • 今天我们说说生气。 这个生气不是发怒、气愤的生气,而是生机盎然的生气。 (百度百科中的生气(shēng qì),一...
    声入人心阅读 2,444评论 2 3
  • 你迎着清晨第一束阳光醒来, 你伴着夜里每一颗繁星睡去。 你和着春天的号角而来, 你随着夏天的蝉鸣而歌, 你迎着秋天...
    mm默mm阅读 1,313评论 0 1
  • 陌生人。他/她有的,你没有,心里不舒服,想得到,想体验。不要贪心,这是自寻烦恼。每个人的过往和经历都不同,很有可能...
    元小汤阅读 2,444评论 0 1
  • 又一个美丽的清晨如约而至。今天,天格外的蓝,一切都沐浴在金色的阳光下,显得如此静谧而安详。沉寂了一个假期的...
    小琴读书阅读 3,262评论 2 7