views:

585

answers:

2

Hi,

I am a newbie in Quartz and I am fighting to understand this stuff apple say is very easy and straightforward.

I have created two CGLayers: one for a fixed background and another one for a sprite. I want this sprite to move.

Both the background context and the sprite context are drawn offscreen and I would like both to be seen on the screen.

To do that - and I am not sure if this is the correct way - I have did the following:

  1. I have created an UIImageView
  2. I have captured the layer's contents using

    resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    myView.image = resultingImage;

This shows me on the screen the contents of both quartz layers.

Now I have two problems:

  1. This approach is slow as hell
  2. When I move the layer, I have to repeat the mentioned code and EVEN DOING THIS, THE LAYER IS NOT MOVING!!!!

So, please, iPhone gurus out there, please tell me if there's another way to do this with quartz and what I have to do to see the sprite moving!!!!

thanks for any help!

+1  A: 

The main reason to use CALayers is to get the GPU to composite directly onto the screen memory. Using UIGraphicsGetImageFromCurrentImageContext() results in the image being composited on the GPU, transferred to main memory and then back again to draw it on the screen which will be really slow. What you should be doing instead is making your new layers sublayers of your view's layer:

[self.view.layer addSublayer:myNewLayer];

For a good example of how to use CALayers in a game see: GeekGameBoard

rpetrich
Thanks! The reason why I am using CGLayers is because I am allowing the user to transform the sprite with pinch (zoom, rotation, etc) and I will save the result (background + transformed sprite) to a file at the end. You may say: why don't you use a CALayer, record all transformations done to the sprite and apply them to the high resolution version later offscreen? Yes, that's how I started this, but after 3 days of all sorts of problems, I decided this other approach. I googled around and never found how to do that and after posting the question on several forums nobody ever answered.
Digital Robot
I have tried to read Apple's documentation for Quartz, but as all Apple docs, they are done by brain surgeons to brain surgeons, I mean, you have to be black belt 3rd dan to understand, because the docs are vague and I laugh hysterically to all examples, because instead of doing it simple like, "example of moving a ball from point A to point B", Apple creates an example like "how to create a vector illustration of planet earth with 2 trillion lines of codes"... apple has mania of flooding the developer with 123 trillion docs that no one will ever understand completely. The guys are insane.
Digital Robot
If I understand you correctly, you'd like to overlay a sprite on top of an image, display that and have the user interact with it, then save a higher-resolution version of the composited image? One way to do that is to hierarchically render a CALayer and its sublayers directly into your high-resolution image at the end. We do this for PDF rendering in the Core Plot framework (see the CPLayer class): http://code.google.com/p/core-plot/
Brad Larson
Follow Brad's advice; the method used in Core Plot is both simple and powerful.
rpetrich
Is this for iphone? can the sprite be rendered with a blending mode? I will check it out. thanks
Digital Robot
A: 

I have a similar scenario where I need to draw a path on top of a UIImage and being able to resize both the image and path. The path should be drawn in a resolution-independent manner but this is not happening. Any clues how to solve this?

http://stackoverflow.com/questions/1511879/overlaying-2d-paths-on-uiimage-without-scaling-artifacts

Cheers

tat0

tat0