【问题标题】:how to erase uiimageview and its subviews?如何擦除 uiimageview 及其子视图?
【发布时间】:2011-03-17 10:30:39
【问题描述】:

我想擦除视图中的图像(比如用橡皮擦擦除绘图)。如果视图框架为 0,0,320,480,我会这样做,但我担心视图框架的大小是否不同:

这是我的代码:

UIGraphicsBeginImageContext(self.view.frame.size);
[myImageView.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(),kCGImageAlphaNone); 
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1, 0, 0, 10);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), point.x, point.y);
//this line code for erasing select the part of the image
CGContextClearRect (UIGraphicsGetCurrentContext(), CGRectMake(point.x, point.y, 30, 30)); 
//End the code
CGContextStrokePath(UIGraphicsGetCurrentContext());
myImageView.image = UIGraphicsGetImageFromCurrentImageContext();

【问题讨论】:

  • 如果您将UIGraphicsGetCurrentContext() 缓存在变量中而不是重复调用它,您的代码将更容易阅读。

标签: iphone cocoa-touch ios4


【解决方案1】:

试试下面的代码..

    UIGraphicsBeginImageContext(self.frame.size);
    [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 30.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 1.0, 1.0, 1.0); 
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastTouch.x, lastTouch.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    • 2010-12-02
    • 2022-01-24
    • 2015-09-04
    • 2013-03-29
    相关资源
    最近更新 更多