views:

1054

answers:

1

Hello,

I have a simple UIImageView with an image that is larger then the display (600x1400). The image starts with Fit to Aspect, so the bounds of the UIImgageView report 320x750, still larger then the display, but that's OK. I know of a point (300, 150) relative to the original dimensions of image (600x1400) that I want to scale and position to. If I setup a basic animation to handle the position and scale (zoom, transform) via core animation, the scale happens, but I never end up at the point I want. Instead, it appears I have to calculate the point based on the scale I WILL be at once transformed.

Is there something obvious I'm missing? I've tried using convertPoint:toView, but it always returns the same point I just gave it.

A: 

Yes, when you are setting the origin of a view, you are setting it in the coordinate space of its superview. The superview knows nothing of the scale of the subview, so if you need to move to a coordinate relative to the scaled size of the image, you'll need to scale your coordinate to match. The math to do this is trivial, but you can use the CGPointApplyAffineTransform () function to take a CGAffineTransform that you may have used to scale your UIImageView via its transform property and apply it to a coordinate. (Also, remember that the coordinate 0,0 is in the upper left corner on the iPhone.) I'm not sure if setting the UIImageView to scale to fit will do the scaling via its transform property or not, but you can easily construct an affine transform to match the current scale of your view.

However, I'd suggest that you resample your image to a smaller resolution to save memory and improve performance. 600x1400 is quite a large image to be throwing around on the iPhone. This post has some tips for how to scale UIImages. If you apply a known scale factor to your image, you can simply apply that scale factor to the X and Y components of your desired coordinate.

Brad Larson
Thanks Brad! This is excitingly close and certainly points me in the write direction. My points still aren't going to where I expect, but I think this is a user space vs device space issue, so I'm working with CGContextGetUserSpaceToDeviceSpaceTransform. Matrix math certainly isn't my strength.