2010-06-09 76 views
0

我想繪製個別像素在Xcode輸出到iPhone。我不知道任何OpenGL或Quartz編碼,但我知道一些關於Core Graphics的內容。我正在考慮繪製寬度和高度均爲1的小矩形,但不知道如何將其實現爲代碼以及如何在視圖中顯示該矩形。任何幫助是極大的讚賞。繪製像素 - Objective-C/Cocoa

+4

CoreGraphics中是石英 – justin 2012-01-13 03:03:33

回答

0

所有繪圖都需要進入- (void)drawRect:(CGRect)rect方法。 [self setNeedsDisplay]標記重繪的代碼。問題是你沒有重繪任何東西。

1

如果您希望能夠繪製累積添加到以前繪製的像素的像素,則需要創建自己的位圖圖形上下文,並由您自己的位圖存儲器支持。然後,您可以在位圖存儲器中設置單個像素,或者在圖形上下文中繪製短線或小矩形。要顯示您的繪圖上下文,首先將其轉換爲CGImageRef。然後,您可以將該圖像繪製到視圖的drawRect中的子類UIView中,或將圖像分配給UIView的CALayer的內容。

在Apple文檔中查找:CGBitmapContextCreate和CGBitmapContextCreateImage。

增加:

我寫了,爲什麼你可能需要在iOS應用程序繪製像素時,這樣做更詳細的解釋,再加上一些源代碼片段,在我的博客:http://www.musingpaw.com/2012/04/drawing-in-ios-apps.html

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]; 
} 
+0

剛剛發現和嘗試這個,謝謝!注意:爲了讓它在我的應用程序中繪製到UIView(「drawView」)區域,我必須在viewDidLoad方法中添加PlotView實例作爲子視圖,就像[self.drawView addSubview:self.plotView]; – SonarJetLens 2013-09-11 19:46:36