tags:

views:

423

answers:

1

Hi guys,

I want to add a bit of functionality to my App, and I think that using a CGLayer is the best way of doing it, but have never done any Quartz drawing before (other than some basic tutorials).

My intention is to show a full view of an image that my App will draw (basic geometric shapes based on parameters) in the top half of a view, and show a zoomed in portion of that image in the bottom quarter of the view based on where the user has touched the top image. I want the zoomed in portion to show in a high resolution, so am thinking that I should draw the image onto, say, a 640 x 640 CGLayer, then draw that full layer in the top 320 x 320 half of the view (so scaled down to fit), and the relevant portion touched by the user in the bottom left 160 x 160 of the view, but unscaled, so you'd see a quarter of the main image.

When the user moves their finger around the main view, the portion view in the bottom left would be redrawn from the CGLayer, so no new image rendering is taking place, just CGLayer drawing.

Can anyone see any problem with this approach that my inexperienced brain may have missed?

Ta, Jools.

+1  A: 

Look at CALayer and it's contentsRect parameter. It will give you a fairly simple way to do exactly what you're wanting. You can copy the contents of your image view's layer to your zoomed view layer like this:

[[zoomView layer] setContents:[[tapImageView layer] contents]];

Then you set the contentsRect like this:

[[zoomView layer] setContentsRect:CGRectMake(x, y, 0.25, 0.25)];

You would calculate x and y and call this in touchesMoved. Width and height which are set to 0.25 in this code represent what percentage of the image will show in the zoomed view.

Let me know if you need clarification.

Matt Long
Thanks for your response Matt, that makes sense, and looks like the kind of thing I need to do - but something I'm struggling to find information on, is how to create my initial, 640x640 image in the first place, to be used as a source for the zoomed out view, and the zoomed in view. My understanding of CGLayers is that they inherit the size of the context you use when creating them - how do I create a higher resolution layer (640x640) and then draw into it?Jools.
Jools
I wrote some sample code to play around with this. You can download it here: http://www.cimgf.com/files/TapZoom.zip. It needs to have some kinks worked out, but it demonstrates the basic functionality that I was referring to. Additionally, CGLayers are whatever size you make them. Every UIView has a root layer and its size defaults to its parent's size. If you want to create a larger *canvas* to draw on, just create a larger UIView.
Matt Long