如何在构建 UIImageView 时创建图像
我有一个自定义 UIImageView,当它构造时,我想在内存中创建一个或缓存到 iDevice 图像,该图像是特定颜色的填充矩形,然后将其设置为 UIImageView 的 Image 属性。
我知道如何通过重写 Draw 方法在 UIImage 上绘制它,并且它只会在当前 CGContext 上绘制。然而,由于我想在构建 UIImageView 时执行此操作,因此我实际上没有可以绘制的上下文或任何东西,因为它还不可见。
简而言之,我想知道当应用程序在屏幕上显示任何内容之前启动时,是否有一种方法可以在构建自定义 UIImageView 时以编程方式执行此操作。
我可以接受 C# 或 Objective-C 的答案。
I have a custom UIImageView and when it constructed I would like to create an in memory or cached to the iDevice Image that is a Filled Rectangle of a specific color and then set it to the UIImageView's Image property.
I know how to draw this on a UIImage by overriding the Draw method and it would just draw on the current CGContext. However since I want to do this at the time the UIImageView is constructed I don't actually have a context I can draw on or anything since it is not visible yet.
Simply put I am wondering if there is a way to do this programmatically at construction of the custom UIImageView when the app starts before anything is displayed to the screen.
I am okay with answers in C# or Objective-C.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我可以帮助您使用 UIKit / Objective-C 方法。至于.NET,我没有最模糊的地方。 UIKit 有一些有用的函数用于以编程方式生成 UIImages。您需要做的事情如下所示:
只需确保从主线程调用这样的方法即可。这是我能想到的唯一警告。
[编辑] C#/MonoTouch 版本是:
Well, I can help you with the UIKit / Objective-C approach. As for .NET, I don't have the foggiest. UIKit has a few useful functions for programmatically generating UIImages. What you'll need to do is something like the following:
Just make sure you call such a method from the main thread. That's the only caveat I can think of.
[Edit] The C#/MonoTouch version is: