2010-06-09 6 views
0

아이폰에 출력 할 xcode의 픽셀을 개별적으로 그리려고합니다. OpenGL이나 Quartz 코딩에 대해서는 잘 모르겠지만 Core Graphics에 대해서는 약간 알고 있습니다. 너비와 높이가 1 인 작은 직사각형을 그릴 생각 이었지만 이것을 코드로 구현하는 방법과이를 뷰에 표시하는 방법을 모릅니다. 어떤 도움이라도 대단히 감사합니다.드로잉 픽셀 - Objective-C/Cocoa

+4

CoreGraphics는 석영 – justin

답변

0

모든 도면은 - (void)drawRect:(CGRect)rect 방법으로 이동해야합니다. [self setNeedsDisplay] 다시 그리기 코드에 플래그를 지정합니다. 문제는 다시 그리는 것입니다.

1

이전에 그려진 일부 픽셀에 누적 추가 된 픽셀을 그리려면 고유 한 비트 맵 메모리로 백업 된 고유 한 비트 맵 그래픽 콘텍스트를 만들어야합니다. 그런 다음 비트 맵 메모리에서 개별 픽셀을 설정하거나 그래픽 컨텍스트에 짧은 선이나 작은 사각형을 그릴 수 있습니다. 드로잉 컨텍스트를 표시하려면 먼저 CGImageRef로 변환하십시오. 그런 다음이 이미지를 뷰의 drawRect에서 서브 클래 싱 된 UIView로 그릴 수도 있고 이미지를 UIView의 CALayer 내용에 할당 할 수도 있습니다.

Apple 설명서의 CGBitmapContextCreate 및 CGBitmapContextCreateImage를 참조하십시오.

로 볼 수 있습니다 : 사용자 정의를 위해 http://www.musingpaw.com/2012/04/drawing-in-ios-apps.html

3

:

가 내 블로그에 당신이 iOS 앱에 픽셀을 그릴 때이 필요한 이유를 더 긴 설명, 플러스 일부 소스 코드 조각을 쓴 고정 된 크기와 색상의 점을 그릴 수있는 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