3
votes

I created a drawing program. It works great, you can import an image and annotate it with different colors and brush configurations.

My problem is I can't figure out how to allow erasures. I wrote this same app for android and use a PorterDuffXferMode object to erase. I haven't been able to find something similar here in Cocoa.

Am I missing something obvious? I'm a Cocoa nube.

I'm drawing into an NSView using combinations of NSBezierPath and NSRectFill.

Update:

Before reading the responses here I modified my code to use a nested NSView below my original NSView. So my thought was to draw the background image on the bottom Z-order view and do all the graphics on the top NSView. It draws fine, but in my top view I am calling NSClearRect on an area I want to erase and it's white instead of erasing to the bottom layer.

Why doesn't it show through to the bottom layer?

I'm going to look into using CALayers but you'll have to give me some time to digest what you all have written. Like I said before, I'm a noob to objective C and Cocoa.

1
Would help to know how you draw. OpenGL? CoreGraphics? What are you drawing onto/into?DrummerB
you will have to draw 2 images--the original image behind plus another image over that with your annotations. I.e., if you draw over the original image, you have destroyed the information in the original image. You'll have to save a copy of that information somewhere...nielsbot
You might find it helpful to draw into a CGLayer in your context... Look at CGContextBeginTransparencyLayer()...nielsbot
Or use 2 CoreAnimation layers in your view--one with your image (background) and another on top with your annotation image.nielsbot
>It's probably about the same if you use layer backed views--cf. developer.apple.com/library/mac/#documentation/Cocoa/Reference/…nielsbot

1 Answers

0
votes

I'm not sure how the drawing environment (transfer modes) exactly works on Android, but Core Graphics offers all of the standard and Photoshop-style blend modes, etc.

You're not very specific about what your expected "erasing" behavior actually is. To clear a rectangle in a context, use CGContextClearRect(). If you want to clear in other shapes set a clipping mask as shown in this question.

If you want to do something more sophisticated than clearing the area (e.g. want to repaint a background color or image or pattern) then you can look into image masks and blend modes.