MyLocations: p253 (243) Aspect Fill thumbnails [SOLVED] - Elegant Solution

Continuing former discussions about changing the resizing rule of the thumbnail to Aspect Fill

There’s very elegant solution with only 4 lines changed inside the method:

func resizedImage(withBounds bounds: CGSize) -> UIImage {
    let ratio = max(horizontalRatio, verticalRatio) //Change the ratio to the maximum one
    UIGraphicsBeginImageContextWithOptions(bounds, true, 0) //Cut the image to the given bounds
    let originPoint = CGPoint(x: min((bounds.width - newSize.width),, y: min((bounds.height - newSize.height), //Calculating origin point to center the image
    draw(in: CGRect(origin: originPoint, size: newSize)) //Using the originPoint
1 Like

Thanks for your solution!

Now it just put the image to the bottom right corner of the bound. Try to change the originPoint to:

let originPoint = CGPoint(x: min((bounds.width - newSize.width) / 2,, y: min((bounds.height - newSize.height) / 2,

This put it to the center. The simplest way to check it is to test the three versions of the code (one in the book, your solution’s one, and one with my version of the originPoint using panoramic pictures.

This topic was automatically closed after 166 days. New replies are no longer allowed.