2016-10-25 27 views
0

Im使用https://github.com/nestorpopko/NPGradientImage-Swift爲我的標籤給予漸變效果。在將我的代碼轉換爲Swift 3時,我遇到了許多UIImage + Gradient.swift文件問題。任何人都可以幫助我解決它....?NPGradientImage - Swift 3的問題

+0

什麼是您的問題在這裏.... –

+0

從NPGradientImage - 斯威夫特庫的UIImage + Gradient.swift文件其實即時通訊面臨的問題。現在我固定和代碼如下... –

+0

噢好吧好的........ –

回答

0

最後我已經解決了swift3的問題,它的工作正常。只需用下面的代碼替換的UIImage + Gradient.swift

import UIKit 

public extension UIImage 
{ 
static func gradientImage(colors: [UIColor], locations: [CGFloat], size: CGSize, horizontal: Bool = false) -> UIImage { 
    let endPoint = horizontal ? CGPoint(x: 1.0, y: 0.0) : CGPoint(x: 0.0, y: 1.0) 
    return gradientImage(colors: colors, locations: locations, startPoint: CGPoint.zero, endPoint: endPoint, size: size) 
} 

static func gradientImage(colors: [UIColor], locations: [CGFloat], startPoint: CGPoint, endPoint: CGPoint, size: CGSize) -> UIImage 
{ 
    UIGraphicsBeginImageContext(size) 

    let context = UIGraphicsGetCurrentContext() 
    UIGraphicsPushContext(context!); 

    let components = colors.reduce([]) { (currentResult: [CGFloat], currentColor: UIColor) -> [CGFloat] in 
     var result = currentResult 

     let numberOfComponents = currentColor.cgColor.numberOfComponents 
     let components = currentColor.cgColor.components 


     if numberOfComponents == 2 
     { 
      result += ([(components?[0])!, (components?[0])!, (components?[0])!, (components?[1])!]) 
     } 
     else 
     { 
      result += ([(components?[0])!, (components?[1])!, (components?[2])!, (components?[3])!]) 
     } 

     return result 
    } 
    let gradient = CGGradient(colorSpace: CGColorSpaceCreateDeviceRGB(), colorComponents: components, locations: locations, count: colors.count); 

    let transformedStartPoint = CGPoint(x: startPoint.x * size.width, y: startPoint.y * size.height) 
    let transformedEndPoint = CGPoint(x: endPoint.x * size.width, y: endPoint.y * size.height) 
    context?.drawLinearGradient(gradient!, start: transformedStartPoint, end: transformedEndPoint, options: []); 
    UIGraphicsPopContext(); 
    let gradientImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return gradientImage! 
} 
}