2

UIImageViewとそれに設定された画像を含むビューがあります。消しゴムでフォトショップのように画像を消したいです。どうすればこれを達成できますか?また、消去を元に戻すにはどうすればよいですか?

4

3 に答える 3

8

消去する領域がわかっている場合は、同じサイズの新しい画像を作成し、マスクを画像全体から消去する領域を引いたものに設定し、画像全体を新しい画像に描画して、それをとして使用できます。新しいイメージ。元に戻すには、前の画像を使用するだけです。

編集

サンプルコード。画像ビューから消去したい領域が次のように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();
}
于 2009-09-28T16:34:10.670 に答える
1
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();
于 2011-12-08T09:41:58.700 に答える
0

Swift5.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()
于 2021-02-05T10:22:53.393 に答える