您的位置:首页 > 其它

截屏,如何截取自定义范围的图片

2015-06-17 18:46 162 查看
CGRect rect =self.view.frame;

// UIGraphicsBeginImageContext(rect.size);

个人认为使用这个就相当于UIGraphicsBeginImageContextWithOptions(rect.size, NO,
1.0);

选择NO,其他区域透明 选择YES,其他区域不透明,可以看到黑色区域

1.0这个代表缩放倍数为1.0倍

UIGraphicsBeginImageContextWithOptions(rect.size,NO,
2.0);

CGContextRef context =UIGraphicsGetCurrentContext();
[self.view.layerrenderInContext:context];

UIImage *img =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

//你自定义区域的起点,宽,高;
CGRect rect1 =CGRectMake(0,
(100)*2,SCREEN_WIDTH*2
, (SCREEN_HEIGHT/2-20)*2);//你要截取的是CGRectMake(0,
100, SCREEN_WIDTH , (SCREEN_HEIGHT/2-20))这个范围,但是因为上面是2.0放大了,所以你自定义截取的范围都要放大2倍。
之所以本人要放大两倍,是因为1.0的时候,出现了图片不清晰的情况

UIImage * imgeee = [UIImageimageWithCGImage:CGImageCreateWithImageInRect([imgCGImage],
rect1)];
//imgeee就是你所需要的区域截图
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: