我想繪製個別像素在Xcode輸出到iPhone。我不知道任何OpenGL或Quartz編碼,但我知道一些關於Core Graphics的內容。我正在考慮繪製寬度和高度均爲1的小矩形,但不知道如何將其實現爲代碼以及如何在視圖中顯示該矩形。任何幫助是極大的讚賞。繪製像素 - Objective-C/Cocoa
回答
所有繪圖都需要進入- (void)drawRect:(CGRect)rect
方法。 [self setNeedsDisplay]
標記重繪的代碼。問題是你沒有重繪任何東西。
如果您希望能夠繪製累積添加到以前繪製的像素的像素,則需要創建自己的位圖圖形上下文,並由您自己的位圖存儲器支持。然後,您可以在位圖存儲器中設置單個像素,或者在圖形上下文中繪製短線或小矩形。要顯示您的繪圖上下文,首先將其轉換爲CGImageRef。然後,您可以將該圖像繪製到視圖的drawRect中的子類UIView中,或將圖像分配給UIView的CALayer的內容。
在Apple文檔中查找:CGBitmapContextCreate和CGBitmapContextCreateImage。
增加:
我寫了,爲什麼你可能需要在iOS應用程序繪製像素時,這樣做更詳細的解釋,再加上一些源代碼片段,在我的博客:http://www.musingpaw.com/2012/04/drawing-in-ios-apps.html
對於定製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];
}
剛剛發現和嘗試這個,謝謝!注意:爲了讓它在我的應用程序中繪製到UIView(「drawView」)區域,我必須在viewDidLoad方法中添加PlotView實例作爲子視圖,就像[self.drawView addSubview:self.plotView]; – SonarJetLens 2013-09-11 19:46:36
- 1. 繪製像素
- 2. 使用XCode繪製像素
- 3. 按像素繪製pyglet(python)
- 4. 在WPF中繪製像素
- 5. 像素繪製算法
- 6. 在Java中繪製像素圖像
- 7. VB.NET繪製圖像(組像素)
- 8. 如何繪製位圖,但不繪製透明像素?
- 9. Matplotlib:繪製沒有模糊的像素
- 10. 使用JavaScript繪製單個像素
- 11. Java JFrame繪製像素大小矩形
- 12. 在Quartz中繪製單個像素
- 13. 以自定義形狀繪製像素
- 14. 如何繪製屏幕像素
- 15. 在SDL_Surface像素上繪製矩形
- 16. WPF中的快速像素繪製
- 17. QT繪製像素到圖形場景
- 18. 畫布元素無法繪製圖像
- 19. OpenGL Es 2.0 GLKit繪製像素
- 20. 如何在WPF中繪製像素
- 21. 繪製多個像素/矩形
- 22. 繪製1個像素的iOS
- 23. 繪製單個像素W /帆布
- 24. 如何在XNA中繪製2D像素?
- 25. 如何繪製單個像素?
- 26. 繪製單個像素線HTML5畫布
- 27. 最佳像素繪製速度?
- 28. 繪製像素完美紋理部分
- 29. 繪製像素到屏幕,跨平臺
- 30. 用iphone sdk繪製單個像素
CoreGraphics中是石英 – justin 2012-01-13 03:03:33