Rod*_*ley 4 c# objective-c uiimageview xamarin.ios ios
我有一个自定义 UIImageView,当它构造时,我想在内存中创建一个或缓存到 iDevice 图像,该图像是特定颜色的填充矩形,然后将其设置为 UIImageView 的 Image 属性。
我知道如何通过重写 Draw 方法在 UIImage 上绘制它,并且它只会在当前 CGContext 上绘制。然而,由于我想在构建 UIImageView 时执行此操作,因此我实际上没有可以绘制的上下文或任何东西,因为它还不可见。
简而言之,我想知道当应用程序在屏幕上显示任何内容之前启动时,是否有一种方法可以在构建自定义 UIImageView 时以编程方式执行此操作。
我可以接受 C# 或 Objective-C 的答案。
好吧,我可以帮助您使用 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;
}
Run Code Online (Sandbox Code Playgroud)
只要确保从主线程调用这样的方法即可。这是我能想到的唯一警告。
[编辑] 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1450 次 |
| 最近记录: |