ฉันมีฟังก์ชั่นที่ใช้ UIImage และสีและใช้เพื่อส่งคืน UIImage เป็นสีนั้น
เนื่องจากเป็นส่วนหนึ่งของฟังก์ชัน UIImage จะถูกแปลงเป็น CGImage แต่ด้วยเหตุผลบางอย่าง CGImage จึงมีขนาดใหญ่กว่า UIImage ถึง 3 เท่า ซึ่งหมายความว่าผลลัพธ์สุดท้ายคือ 3 เท่าของขนาดที่ควรเป็น
นี่คือฟังก์ชั่น
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 มีขนาดใหญ่เป็น 3 เท่าของ UIImage เสมอ... ฉันสงสัยว่ามันเกี่ยวข้องกับ @2x, @3x ฯลฯ หรือไม่
ดังนั้นคำถามคือเหตุใดจึงเกิดเหตุการณ์เช่นนี้ และจะทำอย่างไรเพื่อให้ได้ภาพที่มีขนาดเท่ากับขนาดที่ฉันเริ่มต้น