一:在圖片上畫(huà)線后生成繪制圖片的方法
- (void)createCorrectHomeWorkImgCallback:(void(^)(UIImage *image))imgCallback{
// 1: imageView 和 image 是根據(jù)固定屏幕寬度等比例縮放的肌幽; 2:imageView 和 drawView 的 frame 是一樣的羹膳;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.correctImgBrowse.currentZoom.imageView.image.size.width, self.correctImgBrowse.currentZoom.imageView.image.size.height),
NO,
self.correctImgBrowse.currentZoom.imageView.image.scale);// scale 1;
[self.correctImgBrowse.currentZoom.imageView.image drawAtPoint:CGPointZero];
[self.correctImgBrowse.currentZoom.imageView.image drawInRect:CGRectMake(0, 0, self.correctImgBrowse.currentZoom.imageView.image.size.width, self.correctImgBrowse.currentZoom.imageView.image.size.height)];// 2880 1728
//生成繪制的圖片
UIImage *textImg = [self.class screenshot:self.correctImgBrowse.currentZoom.drawView orientation:UIDeviceOrientationPortrait usePresentationLayer:YES];
CGFloat rotation = self.correctImgBrowse.currentZoom.drawView.layer.transformRotationZ;
textImg = [textImg imageRotatedByRadians:rotation];
CGFloat selfRw = self.correctImgBrowse.currentZoom.imageView.bounds.size.width / self.correctImgBrowse.currentZoom.imageView.image.size.width;//0.539
CGFloat selfRh = self.correctImgBrowse.currentZoom.imageView.bounds.size.height / self.correctImgBrowse.currentZoom.imageView.image.size.height;//0.539
// 根據(jù)縮放比例計(jì)算 drawView 生成的圖片的大小箩绍;
CGFloat sw = textImg.size.width / selfRw;//768 2880
CGFloat sh = textImg.size.height / selfRh;//1280 1729
[textImg drawInRect:CGRectMake(0, 0, sw, sh)];
UIImage *tmp = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithCGImage:tmp.CGImage scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]; // <UIImage: 0x2805b1030> size {256, 426.66666666666669} orientation 0 scale 3.000000
imgCallback(image);
});
}
二:生成繪制圖片的方法
1:先把drawView生成圖片
+ (UIImage *)screenshot:(UIView *)view orientation:(UIDeviceOrientation)orientation usePresentationLayer:(BOOL)usePresentationLayer
{
CGSize size = view.bounds.size;
CGSize targetSize = CGSizeMake(size.width * view.layer.transformScaleX, size.height * view.layer.transformScaleY);
UIGraphicsBeginImageContextWithOptions(targetSize, NO, [UIScreen mainScreen].scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
[view drawViewHierarchyInRect:CGRectMake(0, 0, targetSize.width, targetSize.height) afterScreenUpdates:NO];
CGContextRestoreGState(ctx);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
2:根據(jù)縮放比例計(jì)算 drawView 生成的圖片的大小
CGFloat sw = textImg.size.width / selfRw;//768 2880
CGFloat sh = textImg.size.height / selfRh;//1280 1729
[textImg drawInRect:CGRectMake(0, 0, sw, sh)];
UIImage *tmp = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithCGImage:tmp.CGImage scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]; // <UIImage: 0x2805b1030> size {256, 426.66666666666669} orientation 0 scale 3.000000
imgCallback(image);
});