I have a UIImageView
of which I'm setting the image dynamically (from a URL). The images may have arbitrary sizes/aspect ratios. I am setting contentMode
to Aspect Fill and view's layer's clipsToBounds
to YES
, and it's displayed correctly. However, I also want to display shadow under the image view. When I set shadow on the image view's layer, I need to set clipsToBounds
to NO
for the shadow to display, which causes the bleeding portions of the image from the view to be displayed. How can I keep the image view size constant (aspect-filled) and enable shadow simultaneously?
One option may involve creating a graphics context and re-drawing the image into that context, getting an image with my desired aspect ratio and setting that image as my image view's image, but that's extra processing/wasted both CPU/GPU and normal-world time (especially if I have a large number of image views with large images).
Another option may involve creating a blank view at the same size of my image view, inserting it below my view in my superview, attaching it to my view dynamically using constraints, and enabling shadow on that view's layer. That also involves creating an extra view just for shadow. Probably a better/more efficient solution than the first one, but still extra work (CPU-wise).
Is there any option that doesn't require extra work that can enable both shadow and aspect-fitting under my condition?