我正在使用CGContext创建一个带有四个给定点的简单正方形(这些点应该是一个完美的正方形)。然而,iPad应用程序不是200px x 200px的平方,而是看起来像是680w乘300h。我错过了什么吗?
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应用程序,它的方向被锁定在格局中(如果这有任何区别)。
答案 0 :(得分: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);