简体   繁体   中英

How the iMessage App resize the images and keep that aspect ratio

it is not clear from the question, I know. but I wanna know how iMessage App resizes the image when we send an image. let me clarify it by showing you..

Here I send an image of size: 1668x2500

But when i measure the saved screenshot by Photoshop as shown by black rectangle it shows: 846x1128

i had to measure the image by the end of the bottom right corner of that point arrow. because image is that big and imessage is masking that image.

在此处输入图像描述

And I tried another images of different sizes as shown in table below: Rendered Size means size on device measured by photoshop.

在此处输入图像描述

As far as i know. Max Size that iMessage is using 846x1143 . it doesn't go beyond that. and it resizes the image in that coordinate space.

Soooo. i wanna know the formula that iMessage is using to resize the image . Please Help

Can only guess what their resizing algorithm but you can get pretty close with a couple of calculations. First set your imageView's content mode to .scaleAspectFill . Create a variable to hold both your desired max width and max height. Compare the image's width to the max width, if the image is wider then divide max width by the image width to get the scaling multiplier and multiply the height by that multiplier. If the width is not greater than the max width, then check if the height is greater than the max height and if it is multiply the scale and the image width. For the calculated width or height, you will want to make sure neither is greater than their respective max value.

let image = UIImage(named: "myImage")
let calculatedSize = calculateSize(for: image)
myImageView.frame = CGRect(origin: myImageView.frame.origin, size: calculatedSize)
myImageView.contentMode =  .scaleAspectFill
myImageView.clipsToBounds = true
//myImageView.layer.cornerRadius = 15

func calculateSize(for image: UIImage?) -> CGSize {

    guard let image = image else { return .zero }

    let widthMax: CGFloat = 260
    let heightMax: CGFloat = 350
    let maxMultiplier: CGFloat = 1.5

    var width: CGFloat = image.size.width
    var height: CGFloat = image.size.height
    var multiplier: CGFloat = 1.0

    print(["original", (width, height)])

    if width < widthMin {

        multiplier = widthMin / width
        width = multiplier * width
        height = multiplier * height

    } else if height < heightMin {

        multiplier = heightMin / height
        width = multiplier * width
        height = multiplier * height

    } else if width < widthMax && width > height {

        let originalWidth = width

        multiplier = widthMax / width
        width = min(maxMultiplier * width, multiplier * width)
        height = (width / originalWidth) * height

    } else if height < heightMax && height >= width {

        let originalHeight = height

        multiplier = heightMax / height
        height = min(maxMultiplier * height, multiplier * height)
        width = (height / originalHeight) * width
    }

    if width >= widthMax {

        multiplier = widthMax / width
        width = widthMax
        height = min(heightMax, height * multiplier)

    } else if height >= heightMax {

        multiplier = heightMax / height
        width = min(widthMax, width * multiplier)
        height = heightMax
    }
    print(["resize", (width, height)])

    return CGSize(width: width, height: height)
}

结果是一个tableView

This is how the algorithm resizes smaller images

调整大小的较小图像

And these are the same iamges sent via Messages

消息如何调整大小

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM