【发布时间】:2011-02-03 21:25:24
【问题描述】:
我想使用 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
MonoDevelop 2.4.2,MonoTouch
我创建了一个新解决方案:C#、iPhone 和 iPad、基于 iPhone 窗口的项目
编辑了 MainWindow.xib:
2a。在窗口中添加了 UIImageView 和 UIButton;
2b。我在 AppDelegate 中添加了两个出口,btnAddText(引用到 UIButton)和 imgImage(引用到 UIImageView);-
代码(抱歉——代码格式受到质疑...):
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) { } } }
【问题讨论】:
-
您是否仔细检查了您的矩形大小?也许它从图像中提取而你看不到它。
标签: iphone xamarin.ios