【问题标题】:How to erase some portion of a UIImageView's image on iOS?如何在 iOS 上擦除 UIImageView 图像的某些部分?
【发布时间】:2010-12-02 00:52:53
【问题描述】:

我有一个带有 UIImageView 的视图和一个为其设置的图像。我想像在 Photoshop 中用橡皮擦一样擦除图像。我如何实现这一目标?另外,如何撤消擦除?

【问题讨论】:

    标签: objective-c iphone core-graphics erase


    【解决方案1】:

    如果您知道要擦除的区域,则可以创建一个相同大小的新图像,将遮罩设置为整个图像减去您要擦除的区域,将整个图像绘制到新图像中,然后使用那作为新的形象。要撤消,只需使用上一张图片即可。

    编辑

    示例代码。假设您要从图像视图imgView 中删除的区域由erasePath 指定:

    - (void) clipImage 
    {
        UIImage *img = imgView.image;
        CGSize s = img.size;
        UIGraphicsBeginImageContext(s);
        CGContextRef g = UIGraphicsGetCurrentContext();
        CGContextAddPath(g,erasePath);
        CGContextAddRect(g,CGRectMake(0,0,s.width,s.height));
        CGContextEOClip(g);
        [img drawAtPoint:CGPointZero];
        imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    

    【讨论】:

    • 感谢示例代码,但在 CGContextAddPath(g,erasePath);如何获得擦除路径?请帮帮我。
    • 嗨@RahulVyas,@Dhaval,@Ed Marty,如果你有的话,你能告诉我你是如何获得 ErasePath 的。
    • 这里是获取“erasePath”的方法。只需将要裁剪的选定 CGRect 传递为“cropRect” - CGMutablePathRef erasePath = CGPathCreateMutable(); CGPathAddRect(erasePath, NULL,cropRect); CGContextAddPath(g,erasePath);
    【解决方案2】:
    UIGraphicsBeginImageContext(imgBlankView.frame.size);
    [imgBlankView.image drawInRect:CGRectMake(0, 0, imgBlankView.frame.size.width, imgBlankView.frame.size.height)];
    
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(),lineWidth);
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
    
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());  
    CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
    
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint1.x, lastPoint1.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    imgBlankView.image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    

    【讨论】:

      【解决方案3】:

      Swift 5.2 版本

              UIGraphicsBeginImageContextWithOptions(imageView.frame.size, false, 1.0)
              let currentContext = UIGraphicsGetCurrentContext()
              currentContext?.addPath(shapeLayer.path!)
              currentContext?.addRect(CGRect(x: 0, y: 0, width: imageView.frame.size.width, height: imageView.frame.size.height))
              currentContext?.clip(using: .evenOdd)
              
              imageView.image?.draw(at: .zero)
              
              let imageTeemp = UIGraphicsGetImageFromCurrentImageContext();
              UIGraphicsEndImageContext()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-17
        • 2016-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多