美文网首页iOS开发实践
iOS 在图片上画线后生成绘制图片

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

作者: 欧大帅Allen | 来源:发表于2019-08-07 11:14 被阅读0次

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

    - (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);
    });
    

    相关文章

      网友评论

        本文标题:iOS 在图片上画线后生成绘制图片

        本文链接:https://www.haomeiwen.com/subject/ffxodctx.html