0

UIButtons イメージの一部を描画します。クリア オプションをクリックした後、ボタン イメージを最初のイメージにリセットする必要があります (描画を削除します)。setImage プロパティで UIButton の画像を設定したのですが、うまくいきませんでした。次のコードを使用して UIImage に描画します。

    UIImageView *imgView =[[UIImageView alloc]init];      
    UIGraphicsBeginImageContext(self.frame.size);
    [imgView.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), self.drawingWidth);


    CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(),lipColor.CGColor);
    CGContextSetAlpha(UIGraphicsGetCurrentContext(), self.currntAlpha);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    CGContextFlush(UIGraphicsGetCurrentContext());
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeOverlay);
    imgView.image = UIGraphicsGetImageFromCurrentImageContext();
    imgView.frame=CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);// Set frame for imageView
    [self.imageView addSubview:imgView];
    UIGraphicsEndImageContext();
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, self.bounds);
    imgView.layer.shadowOffset = CGSizeZero;
    imgView.layer.masksToBounds = NO;        
    imgView.alpha=0.05;
    lastPoint=currentPoint;
    self.layer.shadowOpacity=0.01;

どんな助けでも大歓迎です!!!!!!

4

1 に答える 1

1

簡単な解決策 - 最初に元の画像を保存し、線を消去したい場合は次のようにします。

self.image = savedImage

ただし、はるかに優れたソリューション (より優れたパフォーマンス) は、画像をまったく変更せずに、透明なビューを表示してそのUIImageView内部に描画することです。描画ももっと簡単にできます (たとえば、CGImageRef毎回画像コンテキストを作成する代わりにバッファを使用するなどUIGraphicsBeginImageContext)。

于 2013-09-20T05:23:19.157 に答える