绘制像素 - Objective-C / Cocoa

时间:2010-06-09 15:14:00

标签: iphone cocoa-touch core-graphics pixel

我试图在xcode中绘制单个像素以输出到iphone。我不知道任何OpenGL或Quartz编码,但我对Core Graphics有所了解。我正在考虑绘制宽度和高度为1的小矩形,但不知道如何将其实现到代码中以及如何在视图中显示它。非常感谢任何帮助。

3 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:1)

如果您希望能够绘制累积添加到某些先前绘制的像素的像素,那么您需要创建自己的位图图形上下文,由您自己的位图内存支持。然后,您可以在位图内存中设置单个像素,或在图形上下文中绘制短线或小矩形。要显示绘图上下文,首先将其转换为CGImageRef。然后你可以将这个图像绘制到视图的drawRect中的子类UIView,或者将图像分配给UIView的CALayer的内容。

查找:Apple文档中的CGBitmapContextCreate和CGBitmapContextCreateImage。

增加:

我写了一个更长的解释,说明为什么在我的博客上绘制iOS应用中的像素以及一些源代码片段时可能需要这样做:http://www.musingpaw.com/2012/04/drawing-in-ios-apps.html

答案 2 :(得分:0)

所有绘图都需要进入- (void)drawRect:(CGRect)rect方法。 [self setNeedsDisplay]标记重绘的代码。问题是你没有重绘。