Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top