2011-12-18 3 views
4

나는 간단한 Mac 페인트 응용 프로그램을 크레이트하려고합니다. iOS에서는 touchesMoved 때 UIImageView의 이미지를 업데이트하기 위해 UIGraphicsGetImageFromCurrentImageContext를 사용했습니다. /에 어떤 조언을바탕 화면 코코아의 UIGraphicsGetImageFromCurrentImageContext와 일치합니까?

myNSImageView.image = UIGraphicsGetImageFromCurrentImageContext(); 

을하지만 그 기능은 코코아 프레임 워크 내에서 코코아 터치에 존재하지 않습니다 : 지금 활동 같은 일을하지만, 맥 애플 리케이션에 노력하고있어 내가하고 싶은 코코아에서 비슷한 기능을 찾을 수 있다면?

감사합니다.

업데이트

완전성에 대한 몇 가지 추가 코드.

- (void)mouseDragged:(NSEvent *)event 
{ 

    NSInteger width = canvasView.frame.size.width; 
    NSInteger height = canvasView.frame.size.height; 

    NSGraphicsContext * graphicsContext = [NSGraphicsContext currentContext]; 
    CGContextRef contextRef = (CGContextRef) [graphicsContext graphicsPort]; 

    CGContextRef imageContextRef = CGBitmapContextCreate(0, width, height, 8, width*4, [NSColorSpace genericRGBColorSpace].CGColorSpace, kCGImageAlphaPremultipliedFirst); 

    NSPoint currentPoint = [event locationInWindow]; 

    CGContextSetRGBStrokeColor(contextRef, 49.0/255.0, 147.0/255.0, 239.0/255.0, 1.0); 
    CGContextSetLineWidth(contextRef, 1.0); 

    CGContextBeginPath(contextRef); 
    CGContextMoveToPoint(contextRef, lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(contextRef, currentPoint.x, currentPoint.y); 
    CGContextDrawPath(contextRef,kCGPathStroke); 

    CGImageRef imageRef = CGBitmapContextCreateImage(imageContextRef); 
    NSImage* image = [[NSImage alloc] initWithCGImage:imageRef size:NSMakeSize(width, height)]; 
    canvasView.image = image; 
    CFRelease(imageRef); 
    CFRelease(imageContextRef); 

    lastPoint = currentPoint; 

}

그 결과 나는 그림 뷰의 상부에서 잘 작동하는지 지금 받고 있어요,하지만 나머지 봇.

enter image description here

+0

당신은 그것이 "효과가 없다"고 말하고 있습니다 - 어쩌면 당신은 정확하게 문제를 정확히 말해야합니다. – Till

+1

@ 문제는 분명히 밝혀졌습니다. 그가 언급하는 기능은 IOS 뿐이며 그는 맥등을 찾고 있습니다. – jrturton

+0

@jrturton 감사의 말씀을 전합니다. – Till

답변

2
NSInteger width = 1024; 
NSInteger height = 768; 
CGContextRef contextRef = CGBitmapContextCreate(0, width, height, 8, width*4, [NSColorSpace genericRGBColorSpace].CGColorSpace, kCGImageAlphaPremultipliedFirst); 

CGContextSetRGBStrokeColor(contextRef, 49.0/255.0, 147.0/255.0, 239.0/255.0, 1.0); 
CGContextFillRect(contextRef, CGRectMake(0.0f, 0.0f, width, height)); 

CGImageRef imageRef = CGBitmapContextCreateImage(contextRef); 
NSImage* image = [[NSImage alloc] initWithCGImage:imageRef size:NSMakeSize(width, height)]; 
CFRelease(imageRef); 
CFRelease(contextRef); 
+0

고마워, 일하러 가지 않았다, 나는 CGImageRef 인 CGBitmapContextCreate를 "허용"하지 않는다. – Anders

+0

내가하고있는 것을 바꿨는데, 그것이 당신에게 효과가 있는지 알려주세요. 최소한 유효한 컨텍스트와 nsimage를 얻어야합니다. – Nico

+0

감사합니다. 거의 작동하도록했습니다. 귀하의 코드로 내 대답을 편집했지만, 지금 또 다른 이상한 문제가 있습니다. :) – Anders

2
NSGraphicsContext * graphicsContext = [NSGraphicsContext currentContext]; 
CGContextRef contextRef = (CGContextRef) [graphicsContext graphicsPort]; 

CGContextRef imageContextRef = CGBitmapContextCreate(0, width, height, 8, width*4, [NSColorSpace genericRGBColorSpace].CGColorSpace, kCGImageAlphaPremultipliedFirst); 

당신은 현재의 상황이 있다는 것을, 첫째, 가정하고있어이 이미지처럼 (빨간 부분은 canvasView해야한다) 별도의 컨텍스트를 만듭니다.

CGContextSetRGBStrokeColor(contextRef, 49.0/255.0, 147.0/255.0, 239.0/255.0, 1.0); 
CGContextSetLineWidth(contextRef, 1.0); 

(etc.) 

그리고는 당신이 아닌 컨텍스트 만든 존재는 가정 상황에 그립니다.

CGImageRef imageRef = CGBitmapContextCreateImage(imageContextRef); 

그리고 당신은 당신이 생성에 그린 결코 상황에서 이미지를 만들 수 있습니다.

[NSGraphicsContext currentContext] 및 해당 그래픽 포트를 가져 오려는 시도를 자릅니다. 어쨌든 drawRect: 외부의 현재 컨텍스트가 있다고 가정 할 수 없습니다. 대신, 사용자가 작성한 컨텍스트를 그립니다.

+0

나는 가지고있다. contextRef를 imageContextRef로 대체하려고 시도했지만 아무 것도 그려지지 않았습니다. 그 그림이 NSViewController에서 발생한다는 것을 명확히해야합니다. 위임 메소드 mouseDown 등으로 NSImageView의 사용자 정의 서브 클래스를 작성했습니다.이 사용자 정의 클래스를 하위보기를보기 컨트롤러보기. 그것은 이상한 드로잉 NSImageView 외부에서 작동하는 것 같아요 (비록 마우스를 끌어에서 대리자 메서드를 화재)하지만 안쪽. – Anders

+0

@ Anders : "나는 contextRef 바꿀려고 imageContextRef,하지만 아무것도 그려지지 않습니다. "지금 만들지 않은 컨텍스트에서는 아무것도 그리지 않고 이미지를 추출하려고 시도합니다. 컨텍스트 n에서 이미지를 추출합니다. 어떤 일이 생겨났다. 게다가, 당신이 * 끌어 들이고있는 상황은 얻을 수있는 것이 아닙니다. 뷰의'drawRect :'메소드 (또는 현재 컨텍스트를 설정 * 한 후에 드문 경우를 제외하고는 필요하지 않음)에서 현재 컨텍스트에만 그려야합니다. 다른 곳에서는 그릴 수있는 유일한 올바른 방법은 만드는 컨텍스트입니다. –

+0

자, 조언과 조언에 감사드립니다. 내 접근 방식을 변경하고 내 드로잉 코드를 내 NSImageView 자체로 이동하려고 시도합니다. 이 예제가 작동하는 것 같습니다. http://juliuspaintings.co.uk/cgi-bin/paint_css/animatedPaint/010-NSView.pl. 감사. :) – Anders