2012-02-27 18 views
1

self.bounds混乱

CGRect contextRect = self.bounds。

は、どの境界を参照するか? rectangle、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

CGRect contextRect = self.bounds; 

が同じであるhttps://developer.apple.com/library/ios/documentation /uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW1 –

+1

これをチェックしましたか? http://stackoverflow.com/questions/1210047/iphone-development-whats-the-difference-between-the-frame-and-the-bounds (デュープとしてクローズ?)返信用 –

+0

おかげで、Iフレームとバインディングの違いを既に知っていますが、私のコードに関しては混乱していました。 – Pamy

答えて

5

self.frameは、親ビュー座標系でのビューの座標とサイズを示します。

self.boundsは、独自の座標系でのビューの座標とサイズを示します。したがって、常にwidthheightself.frameとなりますが、xy0になります。

self.boundsは、CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)に等しい。

だからあなたのコード:あなたは常にiOSのリファレンスを参照してください可能性があり

CGRect contextRect = rect; 
+0

今私はself.bounds常にビューを参照してください知っている。 – Pamy

+0

ありがとうございました。 – Pamy