2012-02-27 118 views
1

在此代碼self.bounds混亂

的CGRect contextRect = self.bounds;

會引用哪個邊界?矩形,imageRect或整個iOS視圖。

我想用quartz2D來操作圖像,我通過查看不同的例子創建了這段代碼,並在//之間編寫的代碼來自Apple Quartz2D指南繪製文本。

在此先感謝, 此致敬禮。

- (void)drawRect:(CGRect)rect 
    { 
     CGSize cgs = CGSizeMake(250.0, 400.0); 
     UIGraphicsBeginImageContext(cgs); 

     CGRect rectangle = CGRectMake(0,0,cgs.width,cgs.height); 
     CGRect imageRect = CGRectInset(rectangle, 5.4, 5.4); 
     imageRect.size.height -= 100; 

     UIImage *myImage = [UIImage imageNamed:@"pumpkin.jpg"]; 
     [myImage drawInRect:imageRect];  

     CGContextRef context = UIGraphicsGetCurrentContext(); 
     CGContextSetLineWidth(context, 10.0); 
     CGContextSetRGBStrokeColor(context, 0.0, 0.0, 1.0, 1.0); 
     CGContextStrokeRect(context, rectangle); 

     //  

     CGRect contextRect = self.bounds; 

     CGContextTranslateCTM(context, 0, contextRect.size.height); 
     CGContextScaleCTM(context, 1, -1); 

     float w, h; 
     w = contextRect.size.width; 
     h = contextRect.size.height; 

     CGAffineTransform myTextTransform; 
     CGContextSelectFont (context, "Helvetica-Bold", h/10, kCGEncodingMacRoman); 
     CGContextSetCharacterSpacing (context, 10); 
     CGContextSetTextDrawingMode (context, kCGTextFillStroke); 

     CGContextSetRGBFillColor(context, 0, 1, 0, .5); 
     CGContextSetRGBStrokeColor(context, 0, 0, 1, 1); 
     myTextTransform = CGAffineTransformMakeRotation(0); 
     CGContextSetTextMatrix(context, myTextTransform); 
     CGContextShowTextAtPoint(context, 0, 50, "Quartz 2D", 9); 

     //  

     UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 

     [testImg drawAtPoint:CGPointMake(35, 10)]; 
} 
+0

你總是可以參考參考iOS版:https://developer.apple.com/library/ios/documentation /uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW1 – 2012-02-27 13:26:22

+1

你檢查過了嗎? http://stackoverflow.com/questions/1210047/iphone-development-whats-the-difference-between-the-frame-and-the-bounds (close as dupe?) – 2012-02-27 13:33:16

+0

感謝您的回覆,我已經知道框架和邊界之間的區別,但我對我的代碼感到困惑。 – Pamy 2012-02-27 14:06:37

回答

5

self.frame指示視圖在其父視圖座標系中的座標和大小。

self.bounds指示視圖的座標和大小在其自己的座標系中。所以它總是有相同的widthheight作爲self.frame,但它有xy等於0

self.bounds等於CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)

所以,你的代碼:

CGRect contextRect = self.bounds; 

是一樣的:

CGRect contextRect = rect; 
+0

現在我知道self.bounds始終引用該視圖。 – Pamy 2012-02-27 14:07:20

+0

非常感謝您的幫助。 – Pamy 2012-02-27 14:09:28