2010-07-05 48 views
0

我正在用CGContext創建一個帶有四個給定點的簡單正方形(點應該是一個完美的正方形)。但是,iPad應用的尺寸不是200像素x 200像素,而是由300小時製成的。我錯過了什麼嗎?CGContext的寬度似乎超過了延伸

int beginPointX, beginPointY, gridSize, gridPadding; 

gridSize = 200; 
gridPadding = 10; 

beginPointX = gridPadding; // padding from left border 
beginPointY = gridPadding; // padding from top border 


// initiate UIGraphics method 
UIGraphicsBeginImageContext(self.view.frame.size); 

// set context for our UIGraphics method 
CGContextRef context = UIGraphicsGetCurrentContext(); 

// set some defaults for our CGContext 
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0); 
CGContextSetLineWidth(context, 1.0); 
CGContextBeginPath(context); 


// start 

// build outer box 
CGRect testRect = CGRectMake(beginPointX, beginPointY, (beginPointX + gridSize), (beginPointY + gridSize)); 
CGContextAddRect(context, testRect); 
CGContextStrokePath(context); 

/* 
NSLog(@"Line from %dx%d to %dx%d", beginPointX, beginPointY, beginPointX, (beginPointY + gridSize)); 
CGContextMoveToPoint(context, beginPointX, beginPointY); 
CGContextAddLineToPoint(context, beginPointX, (beginPointY + gridSize)); 
CGContextStrokePath(context); 

NSLog(@"Line from %dx%d to %dx%d", beginPointX, (beginPointY + gridSize), (beginPointX + gridSize), (beginPointY + gridSize)); 
CGContextMoveToPoint(context, beginPointX, (beginPointY + gridSize)); 
CGContextAddLineToPoint(context, (beginPointX + gridSize), (beginPointY + gridSize)); 
CGContextStrokePath(context); 

NSLog(@"Line from %dx%d to %dx%d", (beginPointX + gridSize), (beginPointY + gridSize), (beginPointX + gridSize), beginPointY); 
CGContextMoveToPoint(context, (beginPointX + gridSize), (beginPointY + gridSize)); 
CGContextAddLineToPoint(context, (beginPointX + gridSize), beginPointY); 
CGContextStrokePath(context); 

NSLog(@"Line from %dx%d to %dx%d", (beginPointX + gridSize), beginPointY, beginPointX, beginPointY); 
CGContextMoveToPoint(context, (beginPointX + gridSize), beginPointY); 
CGContextAddLineToPoint(context, beginPointX, beginPointY); 
CGContextStrokePath(context); 
*/ 

// insert set of instructions into our grid pointer 
grid.image = UIGraphicsGetImageFromCurrentImageContext(); 

有一點需要注意的是,我正在研究一種將其方向鎖定到橫向(如果這有什麼區別)的iPad應用程序。

回答

1
CGRect CGRectMake (
    CGFloat x, 
    CGFloat y, 
    CGFloat width, 
    CGFloat height 
); 

所以,這條線

CGRect testRect = CGRectMake(beginPointX, beginPointY, (beginPointX + gridSize), (beginPointY + gridSize)); 

應該

CGRect testRect = CGRectMake(beginPointX, beginPointY, gridSize, gridSize); 
+0

這是我得到在凌晨2:30工作。謝謝! – 2010-07-05 23:48:10