在UIView Xamarin.IOS C#中绘制图像 [英] Draw Image in UIView Xamarin.IOS C#

查看:110
本文介绍了在UIView Xamarin.IOS C#中绘制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在资产目录中有一些pdf图像.我不想直接在UIImageView中使用它,而是想使用CGContext在UIView中绘制它.

I have some pdf images in Asset catalog. Instead of directly using this in UIImageView, I want to draw this in my UIView using CGContext.

类似于 Android.Graphic.Drawable-OnDraw()

IOS中是否有替代方法? 还是我可以参考任何样本?

Is there an alternatives for this in IOS? Or is there any samples that i can refer to?

我还想设置此图像的位置-与Android中的 OnMeasure(int width,int height)类似.

I also want to set the position of this image - similar to OnMeasure(int width, int height) in Android.View

(需要-我想动态绘制图像并在UIView中的触摸位置上编辑图像)

(need - I want to dynamically draw images and edit the images on touch positions in UIView)

请有人对此提供指导. 谢谢.

Could someone guide on this please. Thanks.

推荐答案

在UIView子类的Draw(drawRect:)覆盖中,您可以在当前上下文上进行绘制.在此示例中,我们将整个视图涂成红色,然后从视图中心绘制并缩放一个CGImage,尺寸为100x200:

In your UIView subclass's Draw (drawRect:) override, you can draw on the current context. In this example, we paint the entire view red and then draw and scale a CGImage from the center of the view with a size of 100x200:

public override void Draw(CGRect rect)
{
    var context = UIGraphics.GetCurrentContext();

    context.SetFillColor(UIColor.Red.CGColor);
    context.FillRect(rect);

    var cgImage = UIImage.FromFile("face").CGImage;

    var drawImageToRect = new CGRect(rect.Width / 2, rect.Height / 2, 100, 200);
    context.DrawImage(drawImageToRect, cgImage);
}

回复: iOS绘图概念

如果需要混合使用UIKit和CoreGraphics原点,则可以转换原点(请咨询上面的Apple doc链接中的Coordinate Systems and Drawing in iOS):

If you need to mix UIKit and CoreGraphics origins, you can transform the origin (consult the Coordinate Systems and Drawing in iOS in the Apple doc link above):

public override void Draw(CGRect rect)
{
    var context = UIGraphics.GetCurrentContext();
    context.SaveState();
    context.TranslateCTM(0, rect.Height );
    context.ScaleCTM(1, -1);

    context.SetFillColor(UIColor.Red.CGColor);
    context.FillRect(rect);

    var cgImage = UIImage.FromFile("face").CGImage;
    var drawImageToRect = new CGRect(rect.Width / 2, rect.Height / 2, 100, -200);
    context.DrawImage(drawImageToRect, cgImage);

    context.RestoreState();
}

这篇关于在UIView Xamarin.IOS C#中绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆