0

I'm stuck with something I can't figure out...

My app lets the user zoom/pan a thumbnail image, via a UIScrollView. Then it needs to take the changes the user made in the scrollview and apply them to the same image at a much higher resolution (ie. generate a high-res UIImage that looks the same as the zoomed/panned low-res thumbnail the user touched).

I can see that the scrollview has a zoomscale and contentOffset which I can reuse, but I really can't see how to apply this to my UIImage.

All help much appreciated, thanks!

xaphod
  • 6,392
  • 2
  • 37
  • 45

1 Answers1

1

The zoom scale,contentOffset and frame of the UIScrollView will present a sub rectangle of the thumbnail.

Rescale that rectangle proportionally against the higher res version of your image.

e.g

Your scroller has bounds of 100px x 100px

Your thumbnail is 100px x 100px and is zoomed at 4x with a content offset of (x:100,y:100). You will see a sub rectangle of frame (x:25,y:25,w:25,h:25) against the original thumbnail inside the 100x100 window of the scroller i.e blurry. The width and height comes from the scrollers frame.

Once you flip in a high res image of 1000px x 1000px you are going to want to present the same chunk of the image except now you present (x:250,y:250,w:250,h:250) by setting the zoom to 0.4. contentOffset remains the same.

Note that the zoom of 1x and zero offset which would present the whole thumbnail image is a zoom of 0.1x and zero offset against the higher res.

BUT

You are overthinking the issue. Your container UIImageView does all the work for you. Once you reach your target zoom point simply load the higher res image into the imageView (myImageView.image = hiresImage ) and it will "just work" assuming your contentMode is set to Scale To Fill (UIViewContentModeScaleToFill) or Aspect Fill . The low res image will be replaced by the high res version in exactly the right position.

Warren Burton
  • 17,451
  • 3
  • 53
  • 73
  • Thanks for the help. The reason i don't just replace the low res image with the high one is because the preparation on the high res image is for printing, not for display -- at the end i need a static UIImage that shows the same as what the user did. Any thoughts? Thanks! – xaphod Nov 20 '14 at 07:12
  • Use what I said in the first part then. Apply the visible rect of the thumbnail to your hires version – Warren Burton Nov 20 '14 at 08:05
  • Do you mean still in a UIScrollView, and then somehow ask it to render just its visible part? or don't use scrollview and use drawRect somehow? – xaphod Nov 20 '14 at 08:14
  • I get your problem now - take the rect you derive from your scroll view and use this answer(http://stackoverflow.com/questions/158914/cropping-a-uiimage) to do the crop on your hires image – Warren Burton Nov 20 '14 at 11:57