将UIImage绘制到CurrentContext中

问题描述:

在我的应用程序中,我使用了UIImagePickerController拍摄照片,拍完照片后,我无法使用UIImageView来绘制它,因为我想用手指画一些线条。将UIImage绘制到CurrentContext中

为此我已经写在平局矩形方法这段代码:

- (void)drawRect:(CGRect)rect { 
    [self.myimage drawInRect: rect]; 

    //Disegno rettangolo del tag 
    CGContextRef myContext = UIGraphicsGetCurrentContext(); 

    CGContextSetRGBStrokeColor(myContext, 0.5, 0.5, 0.5, 1.0); 

    CGContextSetLineWidth(myContext, 3.0f); 

    CGContextAddPath(myContext, pathTouches); 

    CGContextStrokePath(myContext); 

} 

和touchesMoved:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 
    CGPoint locationInView = [touch locationInView:self]; 

    // Draw a connected sequence of line segments 
    CGPoint addLines[] = 
    { 
     //.... 
      //some lines 
    }; 

    CGMutablePathRef newPath = CGPathCreateMutable(); 
    CGPathRelease(pathTouches); 
    pathTouches = CGPathCreateMutableCopy(newPath); 
    CGPathRelease(newPath); 

    CGPathAddLines(pathTouches, NULL, addLines, sizeof(addLines)/sizeof(addLines[0])); 

    [self setNeedsDisplay]; 

} 

是否正确调用[self setNeedsDisplay];我每次都进入我的手指,或出现是一个更好的方法来做到这一点?

我已经这样做了,但停留在另一个问题......这里是您的解决方案......

- (void)imagePickerController:(UIImagePickerController *)picker 
    didFinishPickingImage:(UIImage *)image 
       editingInfo:(NSDictionary *)editingInfo 
{ 
// Dismiss the image selection, hide the picker and 
//show the image view with the picked image 
[picker dismissModalViewControllerAnimated:YES]; 
imagePickerController.view.hidden = YES; 
[imagePickerController release]; 
imageView.image = image; 
imageView.hidden = NO; 
[imageView setFrame:CGRectMake(0, 20, 320, 396)]; 
[window bringSubviewToFront:imageView]; 
} 

然后

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
UITouch *touch = [touches anyObject]; 
if ([touch view] == EditImageView) 
{ 
    lastPoint = currentPoint; 
    lastPoint = [touch locationInView:self.view]; 

UIGraphicsBeginImageContext(EditImageView.frame.size); 
[EditImageView.image drawInRect:CGRectMake(0, 0, EditImageView.frame.size.width, EditImageView.frame.size.height)]; 

//sets the style for the endpoints of lines drawn in a graphics context 
ctx = UIGraphicsGetCurrentContext(); 
CGContextSetLineCap(ctx, kCGLineCapRound); 
//sets the line width for a graphic context 
CGContextSetLineWidth(ctx,10.0); 
//set the line colour 
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0); 
//creates a new empty path in a graphics context 
CGContextBeginPath(ctx); 
    CGContextSetAllowsAntialiasing(ctx,TRUE); 
//begin a new path at the point you specify 
CGContextMoveToPoint(ctx, currentPoint.x, currentPoint.y); 
//Appends a straight line segment from the current point to the provided point 
CGContextAddLineToPoint(ctx, lastPoint.x,lastPoint.y); 
//paints a line along the current path 
CGContextStrokePath(ctx); 
EditImageView.image = UIGraphicsGetImageFromCurrentImageContext(); 
    [EditImageView setNeedsDisplay]; 

    CGContextSaveGState(ctx); 
    UIGraphicsEndImageContext(); 
    currentPoint = lastPoint; 
} 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
UITouch *touch = [touches anyObject]; 
if ([touch view] == EditImageView) 
{ 
    currentPoint=[touch locationInView:self.view]; 
} 
} 

,其中当前点和lastPoint被CGPoint在头文件中声明。

+0

嗨,感谢您的代码,我试图在我的应用程序上绘制一个简单的uiview,但不起作用...你能帮助我吗? 谢谢 – ghiboz 2010-06-10 23:15:09