iOS开发实践

iOS 在图片上画线后生成绘制图片

2019-08-07  本文已影响0人  欧大帅Allen

一:在图片上画线后生成绘制图片的方法

- (void)createCorrectHomeWorkImgCallback:(void(^)(UIImage *image))imgCallback{
// 1: imageView 和 image 是根据固定屏幕宽度等比例缩放的; 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
// 根据缩放比例计算 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:根据缩放比例计算 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);
});
上一篇 下一篇

猜你喜欢

热点阅读