Thursday 15 May 2014

ios - Applying Gradient to UIImage Smoothly -


I am trying to apply the shield to UIImage using CoreGraphic; However, my results are not very good I want to make me a black to transparent gradient at the bottom of the image to create a contrast to some of the stories I though, I do not mix well with the image of the shield; You can clearly see segregation in the center The result I see is like this app: How do I apply the shield to get it? (I have to apply it in large amounts of images).

My results:

Enter image description here

Here is my code:

  func imageWithGradient (img: UIImage!) - & gt; UIImage {UIGraphicsBeginImageContext (img.size) var context = UIGraphicsGetCurrentContext () allow img.drawAtPoint (CGPointMake (0, 0)) colorspace = CGColorSpaceCreateDeviceRGB () places: [CGFloat] = [0.50, 1.0] // 1 = opaque / / 0 = transparent let down blue = UIColor (red: 0, green 0, blue: 0, alpha: 0.5) Ksiji color top = UIColor (red: 0, green 0, blue: 0, alpha: 0). Gradient = CGGradientCreateWithColors (colorspace, [Up, Down], places) to startPoint = CGPointMake (img.size.width / 2, 0) to endpoint = CGPointMake (img.size.width / 2, img.size.height) CGContextDrawLinearGradient ( reference, after the downgrade, startpoint, endpoint, 0) to image = UIGraphicsGetImageFromCurrentImageContext () UIGraphicsEndImageContext () return image}  

SWIFT 3

  Funny Cvivibhinntadhari (IMG: UIImage!) - & gt; (: CGPoint (x: 0, y: 0)) on the colorspace = CGColorSpaceCreateDeviceRGB () go places: UIImage {UIGraphicsBeginImageContext (img.size) reference = UIGraphicsGetCurrentContext () img.draw go [CGFloat] = [0.0, 1.0] Let Below = UIColor (red: 0, green: 0, blue: 0, alpha: 0.5). CgColor top = UIColor (red: 0, green: 0, blue: 0, alpha: 0). CgColor colors = [Up, Down] as CFArray to shield = CGGradient (colorsSpace: colorspace, color: color location: they go) places startPoint = CGPoint (X: img.size.width / 2, Y: 0) (the endpoint = CGPoint X: img. size.width / 2, Y: img.size.height) reference KdrawLinearGradient (shield, began: startpoint, finally: endpoint, options: CGGradientDrawingOptions (rawValue: UInt32 (0))) Go image = UIGraphicsGetImageFromCurrentImageContext () UIGraphicsEndImageContext () Return image! }  

No comments:

Post a Comment