为什么我的CGImage是UIImage的3倍 [英] Why is my CGImage 3 x the size of the UIImage

查看:54
本文介绍了为什么我的CGImage是UIImage的3倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,它接受一个UIImage和一种颜色,并使用它来返回仅该颜色的UIImage.

I have a function that takes a UIImage and a colour and uses it to return a UIImage in just that colour.

作为函数的一部分,UIImage转换为CGImage,但是由于某种原因,CGImage的大小是UIImage大小的3倍,这意味着最终结果是其应有大小的3倍.

As part of the function the UIImage is converted to a CGImage but for some reason the CGImage is 3 times the size of the UIImage which means that the final result is 3 times the size that it should be.

这是功能

func coloredImageNamed(name: String, color: UIColor) -> UIImage {

    let startImage: UIImage = UIImage(named: name)!
    print("UIImage W: \(startImage.size.width) H\(startImage.size.height)")

    let maskImage: CGImage = startImage.CGImage!
    print("CGImage W: \(CGImageGetWidth(maskImage)) H\(CGImageGetHeight(maskImage))")

    // Make the image that the mask is applied to (Just a rectangle of the color want to use)
    let colorImageSize: CGSize = CGSizeMake(startImage.size.width, startImage.size.height)
    let colorFillImage: CGImage = getImageWithColor(color, size: colorImageSize).CGImage!

    // Make the mask image into a mask
    let mask: CGImage = CGImageMaskCreate(CGImageGetWidth(maskImage),
        CGImageGetHeight(maskImage),
        CGImageGetBitsPerComponent(maskImage),
        CGImageGetBitsPerPixel(maskImage),
        CGImageGetBytesPerRow(maskImage),
        CGImageGetDataProvider(maskImage), nil, false)!

        // Create the new image and convert back to UIImage, then return
        let masked: CGImage! = CGImageCreateWithMask(colorFillImage, mask);
        let returnImage: UIImage! = UIImage(CGImage: masked)
     return returnImage
}

您可以在前几行中看到我已经使用print()将每个UIImage和CGImage的大小打印到控制台.CGImage始终是UIImage大小的3倍...我怀疑它与@ 2x,@ 3x等有关吗?

You can see in the first few lines I have used print() to print the size of each the UIImage and CGImage to the console. The CGImage is always 3 times the size of the UIImage... I suspect it has something to do with @2x, @3x etc?

所以问题是为什么会发生这种情况,怎么办才能得到与我刚开始使用的图像大小相同的图像?

So the question is why is this happening and what can do to get an Image out that is the same size as the one I start with?

推荐答案

这是因为UIImage具有 scale 属性.这在像素之间进行调节.因此,例如,从180x180 pixel 图像创建的UIImage,但 scale 为3,会自动被视为尺寸为60x60 points .它将报告为60x60的大小,并且在3像素分辨率的屏幕(3像素对应1点)上也看起来不错.而且,正如您正确地猜到的那样,后缀 @ 3x 或资产目录中的对应位置告诉系统在给UIImage形成图像时,给它一个3的 scale

It's because UIImage has a scale property. This mediates between pixels and points. So, for example, a UIImage created from a 180x180 pixel image, but with a scale of 3, is automatically treated as having size 60x60 points. It will report its size as 60x60, and will also look good on a 3x resolution screen where 3 pixels correspond to 1 point. And, as you rightly guess, the @3x suffix, or the corresponding location in the asset catalog, tells the system to give the UIImage a scale of 3 as it forms it.

但是CGImage没有这样的属性;它只是一个位图,即图像的实际像素.因此,由从180x180 pixel 图像创建的UIImage形成的CGImage也是180x180 points .

But a CGImage does not have such a property; it's just a bitmap, the actual pixels of the image. So a CGImage formed from a UIImage created from 180x180 pixel image is 180x180 points as well.

这篇关于为什么我的CGImage是UIImage的3倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆