如何在构建 UIImageView 时创建图像

发布于 2024-10-31 11:16:40 字数 343 浏览 3 评论 0原文

我有一个自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉徒眸意 2024-11-07 11:16:40

好吧,我可以帮助您使用 UIKit / Objective-C 方法。至于.NET,我没有最模糊的地方。 UIKit 有一些有用的函数用于以编程方式生成 UIImages。您需要做的事情如下所示:

- (UIImage*)generateImage {

    UIGraphicsBeginImageContext(someCGSize);
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // Draw whatever you want here with ctx, just like an overridden drawRect.

    UIImage* generatedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return generatedImage;
}

只需确保从主线程调用这样的方法即可。这是我能想到的唯一警告。

[编辑] C#/MonoTouch 版本是:

UIImage GenerateImage ()
{
    UIGraphics.BeginImageContext (new RectangleF (0, 0, 100, 100));
    var ctx = UIGraphics.GetCurrentContext ();

    // Draw into the ctx anything you want.
    var image = UIGraphics.GetImageFromCurrentImageContext ();
    UIGraphics.EndImageContext ();
    return image;
}

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:

- (UIImage*)generateImage {

    UIGraphicsBeginImageContext(someCGSize);
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // Draw whatever you want here with ctx, just like an overridden drawRect.

    UIImage* generatedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return generatedImage;
}

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:

UIImage GenerateImage ()
{
    UIGraphics.BeginImageContext (new RectangleF (0, 0, 100, 100));
    var ctx = UIGraphics.GetCurrentContext ();

    // Draw into the ctx anything you want.
    var image = UIGraphics.GetImageFromCurrentImageContext ();
    UIGraphics.EndImageContext ();
    return image;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文