截取某部分区域生成图片
- (void)cutScreenWithView:(nullable UIView *)view successBlock:(nullable void(^)(UIImage * _Nullable image,NSData * _Nullable imagedata))block {
//1、开启上下文
UIGraphicsBeginImageContextWithOptions(view.layer.size,NO, 0.0);
//2.获取当前上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//3.截屏
[view.layer renderInContext:ctx];
//字符串
NSString * nameStr = @"字符串";
[nameStr drawInRect:CGRectMake(105, 168, 300, 50) withAttributes:@{NSFontAttributeName:kFont(14),NSForegroundColorAttributeName:UIColor.blackColor}]; //画字符串
//4、获取新图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//5.转化成为Data
//compressionQuality:表示压缩比 0 - 1的取值范围
NSData *data = UIImageJPEGRepresentation(newImage, 1);
//6、关闭上下文
UIGraphicsEndImageContext();
//7.回调
block(newImage, data);
}
- (void)drawRect:(CGRect)rect {
[self cutScreenWithView:self.bgImageView successBlock:^(UIImage * _Nullable image, NSData * _Nullable imagedata) {
self.shareImage = image;
}];
}