0

I'm trying to place a background image on a view, using a UIImageView or anything else that will work. I want it to fill the entire view, but maintain the aspect ratio, and allow me to control which side(s) get clipped. I'd like to use Auto Layout if possible (not code).

By setting Auto Layout constraints to keep the UIImageView the full size of the container, and setting Scale Mode to Aspect Fill, I am very close. The only problem is the image is always centered in the view, in that it is cropped on top and bottom in landscape layout and both sides in portrait. I want to be able to control that. I want to be able to "crop just the right side" (keep left edge docked to left of container), or "crop just the top". I have different images and I'll want each one to crop a different way, and possibly I'll allow users to use their own images and select from a few cropping options.

In this image, I currently get the top result, but want the option to get the bottom. The red box shows the visible area in portrait view (roughly):

enter image description here

My question is similar to this one: iOS Aspect Fill Image Resizing with Content Aligned to Left/Top Edge

However, I do not feel it is a duplicate because my needs are different, and I'm also including an image to help explain better. Also, I will not accept an answer of "use different images for portrait vs landscape" because that doesn't really solve the problem. If you understand my question, you would see that you would actually need a different image for every different aspect ratio, pre-cropped in each alignment I want to support. Obviously that's not really a robust solution.

Community
  • 1
  • 1
eselk
  • 6,764
  • 7
  • 60
  • 93
  • Do the other options like top, top left, center, etc. not give you what you want? – rdelmar Sep 11 '14 at 02:13
  • Unless I'm missing some combination, which I hope I am, the left/right/top/bottom type options do not do any scaling. So if my image is 1024x1024 and the device width is 2048 it will not fill the screen. I could use 2048x2048, but then on small screens you would only see a small part of image. I want it to scale as much as possible, but maintain aspect ratio (which I can do those 2), AND let me pick crop direction (can't do yet). – eselk Sep 11 '14 at 04:39
  • I guess Redraw mode might be an option, but would like to hear an answer from someone with experience as I'm worried about performance issues with that.. and it's hard for me to test on lots of devices to measure performance impact. It seems like it might work fine because this is the "full screen" so it isn't going to change size often, only when rotated, so I don't think drawRect would be called very often? – eselk Sep 11 '14 at 04:51
  • Due to other reasons it turns out I'm better of positioning all of my UI with code, instead of using IB, so I guess this ends up not mattering to me, and probably lots of others have gone the same route. IB is so limiting if you want a AAA quality app that really is designed for all device sizes and rotations, and hacks like multiple XIBs or changing view controllers on rotation (as Apple recommends) are full of issues too. – eselk Sep 12 '14 at 22:02

0 Answers0