Able to capture UIImage as-is based on content mod

2019-03-03 23:19发布

问题:

I was wondering if there is a way to capture an UIImage as it appears as-is viewed in an UIImageView based on its contentMode. For example, if I pass a 3:4 aspect ratio to a square UIImageView, and use "Aspect Fit" as the contentMode, then I'd expect to see white letterboxes in the left and right parts the image to fill out the square. Would it then be possible to take whatever appears and save it directly as an new UIImage with the letter boxing?

Thanks!

回答1:

You can capture a "screen shot" of the way a UIImageView is actually displayed by calling CALayer's renderInContext: method.

https://developer.apple.com/library/ios/documentation/graphicsimaging/reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/instm/CALayer/renderInContext: