2012-01-30 81 views
1

嗨我想繪製一些.png圖像存儲在沙箱和輸出文本在UIView。我的代碼是:試圖在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

這是正確的,這正是應該發生的事情(沒有),因爲viewDidLoad不是您在iOS中進行繪製的正確位置。你需要實現不同的方法:

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

UIView實現調用此方法做圖。試圖在其他地方畫屏幕不會產生理想的結果。

+0

坦率地說,我試圖在UIViewController中繪製。我試過你的代碼: - (void)drawRect:(CGRect)rect CGContextRef myContext = UIGraphicsGetCurrentContext(); UIImage * rImage = [UIImage imageNamed:@「years」]; CGRect imageRect = CGRectMake(10,10,300,400); [rImage drawInRect:imageRect]; [rImage release]; 但它沒有畫任何東西。 – NCFUSN 2012-01-30 00:34:37

+0

@Nathan唯一用於在屏幕上繪圖的地方是UIView。如果你把drawRect放在UIViewController中,它將保持「死代碼」:方法不會被調用。您需要繼承UIView並覆蓋它的drawRect,創建該UIView的一個實例,並將該實例賦值給UIViewController的'self.view'。 – dasblinkenlight 2012-01-30 00:46:02

+0

好的。我會試試看。謝謝。 – NCFUSN 2012-01-30 00:50:07