Glattes Anwenden von Gradient auf UIImage

Ich versuche, mit CoreGraphic einen Farbverlauf auf UIImage anzuwenden. Das Ergebnis ist jedoch nicht sehr schön. Ich möchte einen schwarzen bis transparenten Farbverlauf am unteren Rand des Bildes erstellen, um einen Kontrast für das Platzieren von Text zu schaffen. Der Farbverlauf, den ich erzielen konnte, passt jedoch nicht gut zum Bild.ie können deutlich die Trennung in der Mitte seh. Das Ergebnis, das ich suche, sieht wie folgt aus:http: //capptivate.co/2014/02/17/yummly-3 Wie soll ich den Farbverlauf anwenden, um dies zu erreichen? (Ich muss dies auf eine große Anzahl von Bildern anwenden.)

Mein Ergebnis:

Hier ist mein Code:

func imageWithGradient(img:UIImage!) -> UIImage{

    UIGraphicsBeginImageContext(img.size)
    var context = UIGraphicsGetCurrentContext()

    img.drawAtPoint(CGPointMake(0, 0))

    let colorSpace = CGColorSpaceCreateDeviceRGB()
    let locations:[CGFloat] = [0.50, 1.0]
    //1 = opaque
    //0 = transparent
    let bottom = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5).CGColor
    let top = UIColor(red: 0, green: 0, blue: 0, alpha: 0).CGColor

    let gradient = CGGradientCreateWithColors(colorSpace,
        [top, bottom], locations)


    let startPoint = CGPointMake(img.size.width/2, 0)
    let endPoint = CGPointMake(img.size.width/2, img.size.height)

    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0)

    let image = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

    return image
}

Antworten auf die Frage(4)

Ihre Antwort auf die Frage