2011-03-17 76 views
0

我要删除图像视图(如擦除与橡皮擦图)如果视图帧0,0,320,480。我做到了,但我担心,如果视图框有不同的大小:如何清除uiimageview及其子视图?

这里我的代码:

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(); 
如果缓存
+2

您的代码会更容易阅读'UIGraphicsGetCurrentContext()'在一个变量,而不是重复调用它。 – 2011-03-17 10:33:55

回答

0

尝试下面的代码..

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