iPhoneに出力するxcodeで個々のピクセルを描画しようとしています。OpenGLまたはQuartzコーディングについては知りませんが、コアグラフィックスについては少し知っています。幅と高さが1の小さな長方形を描くことを考えていましたが、これをコードに実装する方法と、これをビューに表示する方法がわかりません。どんな助けでも大歓迎です。
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];
}
以前に描画されたいくつかのピクセルに累積的に追加されたピクセルを描画できるようにしたい場合は、独自のビットマップ メモリに支えられた独自のビットマップ グラフィック コンテキストを作成する必要があります。その後、ビットマップ メモリに個々のピクセルを設定したり、グラフィックス コンテキストで短い線や小さな四角形を描画したりできます。描画コンテキストを表示するには、まずそれを CGImageRef に変換します。次に、ビューの drawRect でサブクラス化された UIView にこの画像を描画するか、UIView の CALayer の内容に画像を割り当てることができます。
Apple のドキュメントの CGBitmapContextCreate および CGBitmapContextCreateImage を参照してください。
追加した:
iOS アプリでピクセルを描画するときにこれを行う必要がある理由と、いくつかのソース コードのスニペットについて、ブログで詳しく説明しています: http://www.musingpaw.com/2012/04/drawing-in- ios-apps.html
- (void)drawRect:(CGRect)rect
すべての描画はメソッドに入る必要があります 。[self setNeedsDisplay]
コードに再描画のフラグを立てます。問題は、何も再描画しないことです。