Adding Single image to CGContext background makes tile view on output

Hi,

Currently I am developing a project by using CGContext to draw image on the specific path.

In my sample i have used a class file which is inherited from the UIView,

Inside the Draw override method, I am getting the UIGraphics.GetCurrentContext(), by using this I have drawn the arc of 0 to 360 degree,

The CGContext setFillColor has to be filled with the image, so i have used the below given code.

var image = UIColor.FromPatternImage(imageView);

context.SetFillColor(image.CGColor);

imageView contain a single image, but on the output screen the image get repeated for many times.

Sample link "https: //github.com/xamarin/xamarin-android/files/2300480/TestApp.2.zip" to replicate this issue.

Can any one let me know how to avoid the Tiles of image on the CGContext background?

Thanks and regards,
Selva Kumar V.

Best Answer

  • LandLuLandLu Xamurai
    Accepted Answer

    You can use UIGraphics to resize your image to what you want:

    public override void Draw(CGRect rect)
    {
        base.Draw(rect);
        using (CGContext g = UIGraphics.GetCurrentContext())
        {
            using (CGPath pathStatus12 = new CGPath())
            {
                pathStatus12.MoveToPoint(this.Center);
                pathStatus12.AddArc(200, 400, 500f, 0.0F, 360.0F, false);
                g.AddPath(pathStatus12);
                UIImage imageView = new UIImage("a0.png");                   
    
                // Adjust your image size to the context size. 
                UIGraphics.BeginImageContextWithOptions(new CGSize(500, 1000), false, 0);
                imageView.Draw(new CGRect(0, 0, 500, 1000));
                UIImage resultImage = UIGraphics.GetImageFromCurrentImageContext();
                UIGraphics.EndImageContext();
    
                var image = UIColor.FromPatternImage(resultImage);
    
                g.SetFillColor(image.CGColor);
                g.DrawPath(CGPathDrawingMode.Fill);                    
            }
        }
    }
    

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    You can use UIGraphics to resize your image to what you want:

    public override void Draw(CGRect rect)
    {
        base.Draw(rect);
        using (CGContext g = UIGraphics.GetCurrentContext())
        {
            using (CGPath pathStatus12 = new CGPath())
            {
                pathStatus12.MoveToPoint(this.Center);
                pathStatus12.AddArc(200, 400, 500f, 0.0F, 360.0F, false);
                g.AddPath(pathStatus12);
                UIImage imageView = new UIImage("a0.png");                   
    
                // Adjust your image size to the context size. 
                UIGraphics.BeginImageContextWithOptions(new CGSize(500, 1000), false, 0);
                imageView.Draw(new CGRect(0, 0, 500, 1000));
                UIImage resultImage = UIGraphics.GetImageFromCurrentImageContext();
                UIGraphics.EndImageContext();
    
                var image = UIColor.FromPatternImage(resultImage);
    
                g.SetFillColor(image.CGColor);
                g.DrawPath(CGPathDrawingMode.Fill);                    
            }
        }
    }
    
Sign In or Register to comment.