1

您好我正在尝试绘制一些存储在沙箱中的 .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 的画布上看不到任何图像和文本。这里有什么问题?

4

1 回答 1

4

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

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

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

于 2012-01-30T00:11:04.580 回答