我正在尝试在 xcode 中绘制单个像素以输出到 iphone。我不知道任何 OpenGL 或 Quartz 编码,但我对 Core Graphics 有一点了解。我正在考虑绘制宽度和高度为 1 的小矩形,但不知道如何在代码中实现它以及如何让它在视图中显示。任何帮助是极大的赞赏。
问问题
5921 次
3 回答
3
对于允许绘制固定大小和颜色的点的自定义 UIView 子类:
// Make a UIView subclass
@interface PlotView : UIView
@property (nonatomic) CGContextRef context;
@property (nonatomic) CGLayerRef drawingLayer; // this is the drawing surface
- (void) plotPoint:(CGPoint) point; //public method for plotting
- (void) clear; // erases drawing surface
@end
// implementation
#define kDrawingColor ([UIColor yellowColor].CGColor)
#define kLineWeight (1.5)
@implementation PlotView
@synthesize context = _context, drawingLayer = _drawingLayer;
- (id) initPlotViewWithFrame:(CGRect) frame; {
self = [super initWithFrame:frame];
if (self) {
// this is total boilerplate, it rarely needs to change
self.backgroundColor = [UIColor clearColor];
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat width = frame.size.width;
CGFloat height = frame.size.height;
size_t bitsPerComponent = 8;
size_t bytesPerRow = (4 * width);
self.context = CGBitmapContextCreate(NULL, width, height, bitsPerComponent, bytesPerRow, colorspace, kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(colorspace);
CGSize size = frame.size;
self.drawingLayer = CGLayerCreateWithContext(self.context, size, NULL);
}
return self;
}
// override drawRect to put drawing surface onto screen
// you don't actually call this directly, the system will call it
- (void) drawRect:(CGRect) rect; {
// this creates a new blank image, then gets the surface you've drawn on, and stamps it down
// at some point, the hardware will render this onto the screen
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGImageRef image = CGBitmapContextCreateImage(self.context);
CGRect bounds = [self bounds];
CGContextDrawImage(currentContext, bounds, image);
CGImageRelease(image);
CGContextDrawLayerInRect(currentContext, bounds, self.drawingLayer);
}
// simulate plotting dots by drawing a very short line with rounded ends
// if you need to draw some other kind of shape, study this part, along with the docs
- (void) plotPoint:(CGPoint) point; {
CGContextRef layerContext = CGLayerGetContext(self.drawingLayer); // get ready to draw on your drawing surface
// prepare to draw
CGContextSetLineWidth(layerContext, kLineWeight);
CGContextSetLineCap(layerContext, kCGLineCapRound);
CGContextSetStrokeColorWithColor(layerContext, kDrawingColor);
// draw onto surface by building a path, then stroking it
CGContextBeginPath(layerContext); // start
CGFloat x = point.x;
CGFloat y = point.y;
CGContextMoveToPoint(layerContext, x, y);
CGContextAddLineToPoint(layerContext, x, y);
CGContextStrokePath(layerContext); // finish
[self setNeedsDisplay]; // this tells system to call drawRect at a time of it's choosing
}
- (void) clear; {
CGContextClearRect(CGLayerGetContext(self.drawingLayer), [self bounds]);
[self setNeedsDisplay];
}
// teardown
- (void) dealloc; {
CGContextRelease(_context);
CGLayerRelease(_drawingLayer);
[super dealloc];
}
于 2012-04-14T03:51:53.723 回答
1
如果您希望能够绘制累积添加到某些先前绘制的像素的像素,那么您将需要创建自己的位图图形上下文,并由您自己的位图内存支持。然后,您可以在位图内存中设置单个像素,或在图形上下文中绘制短线或小矩形。要显示您的绘图上下文,首先将其转换为 CGImageRef。然后,您可以将此图像绘制到视图的 drawRect 中的子类 UIView,或者将图像分配给 UIView 的 CALayer 的内容。
在 Apple 的文档中查找:CGBitmapContextCreate 和 CGBitmapContextCreateImage。
添加:
我在我的博客上写了一个更长的解释,解释为什么在 iOS 应用程序中绘制像素时可能需要这样做,以及一些源代码片段:http: //www.musingpaw.com/2012/04/drawing-in- ios-apps.html
于 2012-04-14T00:03:08.277 回答
0
所有绘图都需要进入该 - (void)drawRect:(CGRect)rect
方法。[self setNeedsDisplay]
标记代码以进行重绘。问题是你什么都没有重绘。
于 2010-07-02T14:21:30.173 回答