无法使用DrawString(MonoTouch)将文本添加到UIImageView

1 iphone xamarin.ios


我想使用C#和MonoTouch将文本写入iPhone上的图像.DrawImage方法似乎很简单,但是当我运行它时会出错.我错过了什么?任何帮助是极大的赞赏!
提前致谢.

错误:

Thu Feb  3 12:40:34 MacMini.local ImageTextMT[3877] <Error>: CGContextSetFont: invalid context 0x0  
Thu Feb  3 12:40:34 MacMini.local ImageTextMT[3877] <Error>: CGContextSetTextMatrix: invalid context 0x0  
Thu Feb  3 12:40:34 MacMini.local ImageTextMT[3877] <Error>: CGContextSetFontSize: invalid context 0x0  
Thu Feb  3 12:40:34 MacMini.local ImageTextMT[3877] <Error>: CGContextSetTextPosition: invalid context 0x0  
Thu Feb  3 12:40:34 MacMini.local ImageTextMT[3877] <Error>: CGContextShowGlyphsWithAdvances: invalid context 0x0 
Run Code Online (Sandbox Code Playgroud)

MonoDevelop 2.4.2,MonoTouch

  1. 我创建了一个新的解决方案:C#,iPhone和iPad,基于iPhone Window的项目

  2. 编辑MainWindow.xib:
    2a.在窗口中添加了UIImageView和UIButton;
    2B.在AppDelegate中我添加了两个出口,btnAddText(引用UIButton)和imgImage(引用UIImageView);

  3. 代码(抱歉 - 代码格式挑战...):

    using System;
    using System.Collections.Generic;
    using MonoTouch.Foundation;
    using MonoTouch.UIKit;
    using MonoTouch.CoreGraphics;
    using System.Drawing;
    
    namespace ImageTextMT;
    
    {
    public class Application
        {
            static void Main (string[] args)
            {
        UIApplication.Main (args);
            }
        }
    
    // The name AppDelegate is referenced in the MainWindow.xib file.
        public partial class AppDelegate : UIApplicationDelegate
        {
        // This method is invoked when the application has loaded its UI and its ready to run
        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
            {
                // If you have defined a view, add it here:
                // window.AddSubview (navigationController.View);
            window.MakeKeyAndVisible ();
    
            // Add the events for the controls
                btnAddText.TouchDown += btnAddText_Click;
    
                imgImage.Image = UIImage.FromFile("/Users/ParisRat/Projects/ImageTextMT/ImageTextMT/images/testblock.png");
                return true;
            }
    
            protected void btnAddText_Click (object sender, EventArgs e)
            {
                // Define a rectangle
                System.Drawing.RectangleF rect = new System.Drawing.RectangleF(10.0f, 5.0f, 20.0f, 25.0f);
    
                // *** Write text on to the image ***
                this.imgImage.DrawString("HELLO",rect,UIFont.SystemFontOfSize(15.0f), UILineBreakMode.CharacterWrap, UITextAlignment.Left);
            }
    
    
            // This method is required in iPhoneOS 3.0
            public override void OnActivated (UIApplication application)
            {
            }
        }
        }
    
    Run Code Online (Sandbox Code Playgroud)

小智 5

你有两个选择.如果你想使用imgImage.DrawString方法,你需要先将ImageView子类化,重写Draw方法并在那里调用imgImage.DrawString方法.

第二个选项是创建自己的位图上下文并在那里绘制.在monotouch.dialog中可以找到一个很好的例子.有一个自定义绘制的日历.这是来自那里的代码.

public static UIImage MakeCalendarBadge (UIImage template, string smallText, string bigText) {
        using (var cs = CGColorSpace.CreateDeviceRGB ()){
            using (var context = new CGBitmapContext (IntPtr.Zero, 57, 57, 8, 57*4, cs, CGImageAlphaInfo.PremultipliedLast)){
                //context.ScaleCTM (0.5f, -1);
                context.TranslateCTM (0, 0);
                context.DrawImage (new RectangleF (0, 0, 57, 57), template.CGImage);
                context.SetRGBFillColor (1, 1, 1, 1);

                context.SelectFont ("Helvetica", 10f, CGTextEncoding.MacRoman);

                // Pretty lame way of measuring strings, as documented:
                var start = context.TextPosition.X;                 
                context.SetTextDrawingMode (CGTextDrawingMode.Invisible);
                context.ShowText (smallText);
                var width = context.TextPosition.X - start;

                var ns = new NSString (smallText);
                UIFont ff = UIFont.FromName ("Helvetica", 10);

                context.SetTextDrawingMode (CGTextDrawingMode.Fill);
                context.ShowTextAtPoint ((57-width)/2, 46, smallText);

                // The big string
                context.SelectFont ("Helvetica-Bold", 32, CGTextEncoding.MacRoman);                 
                start = context.TextPosition.X;
                context.SetTextDrawingMode (CGTextDrawingMode.Invisible);
                context.ShowText (bigText);
                width = context.TextPosition.X - start;

                context.SetRGBFillColor (0, 0, 0, 1);
                context.SetTextDrawingMode (CGTextDrawingMode.Fill);
                context.ShowTextAtPoint ((57-width)/2, 9, bigText);

                context.StrokePath ();

                return UIImage.FromImage (context.ToImage ());
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)