2011-03-28 66 views
5

如何使用monotouch繪製到現有的UIImage?Draw into UIImage

予加載圖像:UIImage.FromFile(「MyImage.png」)

然後我想繪製的字符串和一些行到該圖像。

有沒有人有代碼示例?

THX

回答

10

這裏是做它的方法:

private void drawOnTouch(object data) 
{ 

    UITouch touch = data as UITouch; 

    if (null != touch) 
    { 

     UIGraphics.BeginImageContext(this.Image == null ? this.Frame.Size : this.Image.Size); 

     using (CGContext cont = UIGraphics.GetCurrentContext()) 
     { 

      if (this.Image != null) 
      { 

       cont.TranslateCTM(0f, this.Image.Size.Height); 
       cont.ScaleCTM(1.0f, -1.0f); 
       cont.DrawImage(new RectangleF(0f,0f,this.Image.Size.Width, this.Image.Size.Height), this.Image.CGImage); 
       cont.ScaleCTM(1.0f, -1.0f); 
       cont.TranslateCTM(0f, -this.Image.Size.Height); 


      } //end if 

      PointF lastLocation = touch.PreviousLocationInView(this); 
      PointF pt = touch.LocationInView(this); 
      using (CGPath path = new CGPath()) 
      { 

       cont.SetLineCap(CGLineCap.Round); 
       cont.SetLineWidth(3); 
       cont.SetRGBStrokeColor(0, 2, 3, 1); 
       path.MoveToPoint(lastLocation.X, lastLocation.Y); 
       path.AddLines(new PointF[] { new PointF(lastLocation.X, 
           lastLocation.Y), 
          new PointF(pt.X, pt.Y) }); 
       path.CloseSubpath(); 

       cont.AddPath(path); 
       cont.DrawPath(CGPathDrawingMode.FillStroke); 
       this.Image = UIGraphics.GetImageFromCurrentImageContext(); 

      }//end using path 


     }//end using cont 
     UIGraphics.EndImageContext(); 
     this.SetNeedsDisplay(); 

    }//end if 


}//end void drawOnTouch 

如果放置在的UIImageView的子類此方法,並從的touchesBegan和TouchesMoved調用它,當你觸摸屏幕將畫在圖像上。