尝试在 UIView 中绘制

发布于 2024-12-29 19:45:50 字数 788 浏览 3 评论 0原文

您好,我正在尝试绘制一些存储在沙箱中的 .png 图像,并在 UIView 中输出文本。我的代码是:

-(void)setItemDetails:(ItemShow *)itmShow
{
   if(theItem!=itmShow)
    {
      [theItem release];
       theItem=itmShow;
      [theItem retain];
    }
  UIImage *rImage=[UIImage imageNamed:@"years"];
[[UIColor blackColor] set];
[rImage drawInRect:CGRectMake(55.0, 22.0, 17.0, 17.0)];

[[UIColor brownColor] set];

[theItem.itemYear drawAtPoint:CGPointMake(7.0,19.0)
                forWidth:100
                withFont:[UIFont systemFontOfSize:17.0]
             minFontSize:17.0
          actualFontSize:NULL
           lineBreakMode:UILineBreakModeTailTruncation 
      baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
}

在我在viewDidLoad中调用这个方法之后。什么也没发生。我在 UIView 的画布上看不到任何图像和文本。这是怎么回事?

Hi I am trying to draw some .png images stored in sandbox and the output the text in UIView. My code is:

-(void)setItemDetails:(ItemShow *)itmShow
{
   if(theItem!=itmShow)
    {
      [theItem release];
       theItem=itmShow;
      [theItem retain];
    }
  UIImage *rImage=[UIImage imageNamed:@"years"];
[[UIColor blackColor] set];
[rImage drawInRect:CGRectMake(55.0, 22.0, 17.0, 17.0)];

[[UIColor brownColor] set];

[theItem.itemYear drawAtPoint:CGPointMake(7.0,19.0)
                forWidth:100
                withFont:[UIFont systemFontOfSize:17.0]
             minFontSize:17.0
          actualFontSize:NULL
           lineBreakMode:UILineBreakModeTailTruncation 
      baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
}

That after I call this method in viewDidLoad. Nothing happens. I can't see any images and text on canvas of UIView. What's wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

带上头具痛哭 2025-01-05 19:45:50

没错,这正是应该发生的事情(什么也没有),因为 viewDidLoad 不是您在 iOS 中绘图的正确位置。您需要实现一个不同的方法:

- (void)drawRect:(CGRect)rect {
CGContextRef myContext = UIGraphicsGetCurrentContext();
    // Do your drawing in myContext
}

调用 UIView 实现的此方法来进行绘图。尝试从其他任何地方在屏幕上绘图不会产生所需的结果。

That's right, it is exactly what's supposed to happen (nothing), because viewDidLoad is not the right place from which you do drawing in iOS. You need to implement a different method:

- (void)drawRect:(CGRect)rect {
CGContextRef myContext = UIGraphicsGetCurrentContext();
    // Do your drawing in myContext
}

This method of your UIView implementation gets called to do the drawing. Trying to draw on the screen from about anywhere else is not going to produce the desired results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文