can i use CGAddLineToPoint and CGAddMoveToPoint in EAGLView - iphone

Error is like this:-
errror:-CGAddLineToPoint invalid context

I assume by EAGLView, you mean a custom UIView subclass that is backed by a CAEAGLLayer. Because such a view has its content drawn by the OpenGL layer, you can't do the same sort of Quartz drawing within it that you would in a UIView backed with a standard CALayer.
My recommendation is to move your custom Quartz drawing to another UIView subclass, and place that UIView as a subview of your CAEAGLLayer-hosting view.

Related

iPhone: In a custom view, how to draw something on top of everything else in the view?

So I made a UIView subclass let's called it 'MyView' that simply draws a line inside the DrawRect method.
I then drag a UIImageView in interface builder and put it inside MyView. But now when I run the program it's obscuring the line that I'm drawing. I'm wondering, is there a way for me to draw the line on top of the image that I have dragged into my View?
No, you'll have to reorganise the view hierarchy. CoreGraphics uses a painters model so views behind will always be drawn over by views infront. To solve this you could use a container view that holds your image view and has a transparent view (line drawing view) that sits over the image view.
Another option is to draw the image using core graphics calls in your drawRect method and then draw the line over it.
One way is to have whatever adds MyView to add your UIImageView on top of MyView. It's not ideal (you probably have good reasons for keeping MyView and the UIImageView in a single class) but definitely solves your problem (I recently did this, only, I added it to the keyWindow =)
I would use a CAShapeLayer that has a path representing the line, and insert that layer ahead of the built-in view layer. Then it should draw on top.

Drawing a line in a UIView subview

I have a UIView subclass. This subclass has multiple subviews. Is it possible to draw a line using core graphics inside a subview that is part of the uiview subclass?
For example, I have a SampleView class, which is a subclass of UIView. Inside this class's header file is the property for UIView *sampleSubView, which is a subview of SampleView. Is it possible to draw a line inside of sampleSubView from the SampleView class implementation?
Thanks for your help!
Josh
If you are asking how a UIView subclass can draw a line on itself, then see the Quartz demo sample code. Basically, you'll override the view's drawRect: method, get the current graphics context, then draw whatever you like onto it.
If you are asking how one view can draw a line on another view, perhaps you need to rethink your architecture.

CALayer and UIView display order

I have a CATiledLayer within a UIView and the UIView also contains a subview.
How can I make sure that the subview is always drawn above the layer?
Most of the time I get the tile layer covering the subview.
By default all layers (hence views) added in the last are drawn on the top. You can change the default with -insertSublayer:below: and similar methods:
[view.layer insertSublayer:tiledLayer below:thatSubview.layer]

what is the context passed into drawLayer:inContext:?

In Iphone development, I want to draw context in layer.
Then the question is:
What exactly is the context passed into drawLayer:inContext:? Is it the layer's contents's context or the UIview's context?
If this is the UIView's context, which UIView it is?
Thanks in advance.
The context being passed in belongs to the CALayer also returned by that delegate method. Usually, this is a display context, but it can also be an image or PDF context if the layer is manually drawn using -renderInContext:.
CALayers can exist on their own, or be used as the backing for a UIView. All UIViews have a layer behind them, which handles the actual display of that view's content. Drawing in a view actually draws on its layer, and, likewise, drawing in a CALayer that backs a UIView will appear to draw to the view.
As I said, you can create CALayers that exist as separate entities, and add them to existing layers as sublayers for display. At some point, there will need to be a UIView that hosts all of these sublayers within its backing layer in order for these layers to be seen on the iPhone's screen.
Note that according to the UIView class reference:
Since the view is the layer’s
delegate, you should never set the
view as a delegate of another CALayer
object. Additionally, you should never
change the delegate of this layer.
This means that for a UIView's layer, you would be handling the delegate method within the UIView in almost all cases, so the layer passed in to that method would be the view's layer. Sublayers can have anything as their delegate, because they are not attached to a particular view.
There is some information here: Providing Layer Content
If you must draw the layer’s content rather than loading it from an image, you implement the drawLayer:inContext: delegate method. The delegate is passed the layer for which content is required and a CGContextRef to draw the content in.
So normally is the context of your delegate object. In the case of UIVIew, the view itself is the delegate.

How can I blend between my UIView and my EAGLView?

I have an opengl scene rendering on an EAGLView layer and some other elements (circles and such) rendering on a UIView (which is a sibling of the EAGLView, positioned above it). Is it possible to blend colors between the two layers? I'd like to do some difference blending to get an inversion effect on the colors from EAGLView.
I've been playing around with CGBlendMode but it only seems to affect what I'm drawing in that current view. I think this has something to do with the CGContext but I'm a little hazy on the details, can I force the UIView and the EAGLView to have the same CGContext so that the blending works between them?
Help, corrections, clarifications are all appreciated. Thanks in advance,
-S
Short answer is you can not. Long answer follows.
By EAGLView you must mean the subclass of UIView that is included in the OpenGL ES Template in Xcode. What makes this class special is that the layerClass class method is overridden and return the CAEAGLLayer class instead of the CALayer class, as is default.
UIView and CALayer work in pairs. All UIView objects are backed by a CALayer, the CALayer is the object responsible for layout and rendering to screen. The UIView is a delegate to the CALayer, and is responsible for drawing it's graphics when needed.
CALayer will let it's delegate (the UIView) draw into a CGContextRef. It is one context per UIView, so you can not use CGBlendMode to blend several views since it will only function within one single UIView context.
Blending of CALayer should be done using the filter properties. These are defined for iPhone OS but the available filters are undefined according to the documentation. This is because Core Image is not available on iPhone OS at this time.
I don't think you'll be able to blend colours in that sense. The best you can do is have one completely obscuring the other, or have the top layer semi-transparent (in which case, you'll see the part underneath) - but you won't be able to do XOR type drawing.