iOS CGFloat用于图像绘制循环

时间:2016-01-22 20:34:40

标签: ios uiimage uigraphicscontext

我想绘制一个网格,它将填充iOS设备的整个屏幕,其中正方形形成一个渐变,这是我的方法:

static func getGradientImage(fromHue first_hue: CGFloat, toHue second_hue: CGFloat) -> UIImage{
        UIGraphicsBeginImageContextWithOptions(CGSize(width: SystemDefaults.screen.width, height: SystemDefaults.screen.height), false, 0)
        let context = UIGraphicsGetCurrentContext()

        let pixel: CGFloat = SystemDefaults.screen.height / 16
        let raw_cols: CGFloat = SystemDefaults.screen.width / pixel
        let cols = Int(raw_cols + 0.4) //Because for iPhone 4 this number is 10.666 and converting it to Int gives 10, which is not enough. It does not affect other screens.
        let width: CGFloat = SystemDefaults.screen.width / CGFloat(cols)

        for index in 0..<16{
            let mlt_row = CGFloat(16 - index)
            for idx in 0..<cols{
                let mlt_col = CGFloat((idx + 5) * (16 / cols))


                let rct = CGRect(x: CGFloat(idx) * width, y: CGFloat(index) * pixel, width: width, height: pixel) //That's where I think the problem is. Coordinates should be more even
                let hue = (first_hue * mlt_row + second_hue * mlt_col) / (mlt_row + mlt_col)
                let clr = UIColor(hue: hue, saturation: 0.85, brightness: 1, alpha: 1)

                CGContextSetFillColorWithColor(context, clr.CGColor)
                CGContextFillRect(context, rct)
            }
        }

        let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return result
    }

问题是输出图像在列之间有微小的线条我认为它是因为width参数是一个复杂的CGFloat并且系统无法移动&#34;恰好是那个距离的下一个rct。这就是因为,例如,iPhone 5的屏幕并不完全是16/9,而是16/9。(某事)。我应该如何绘制图像,以便在屏幕上填充144个(对于iPhone5)正方形,而它们之间没有线条?

0 个答案:

没有答案