NPGradientImage-Swift 3的问题

阿比拉米巴拉

为我的标签使用https://github.com/nestorpopko/NPGradientImage-Swift以提供渐变效果。在将代码转换为Swift 3时,我遇到了UIImage + Gradient.swift文件的许多问题。谁能帮我修复它。...?

阿比拉米巴拉

最后,我用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!
}
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Swift主从模板问题(iOS 8 beta 3)

来自分类Dev

Swift 2或3中的Google Analytics(分析)问题

来自分类Dev

更新到Swift 3后的问题

来自分类Dev

解决Swift 3中缺乏递归协议约束的问题

来自分类Dev

在Swift 3中使用withMemoryRebound的问题

来自分类Dev

iOS 10和Swift 3转换问题

来自分类Dev

队列和Swift 3问题

来自分类Dev

转换为Swift 3问题

来自分类Dev

带有Swift 3问题的DTCoreText

来自分类Dev

Swift 3:AVVideoCompositionCoreAnimationTool问题在视频上添加水印

来自分类Dev

GooglePlaces API Swift 3问题

来自分类Dev

如何解决Swift 3中的键盘问题?

来自分类Dev

从Swift 2.3迁移到Swift 3时出现的问题

来自分类Dev

从Swift 2转换为Swift 3的奇怪问题

来自分类Dev

swift3解析与模型问题

来自分类Dev

检索数据:Firebase和Swift 3问题

来自分类Dev

Swift 3 UITableView问题

来自分类Dev

Swift 3 Firebase DB 读取数据问题

来自分类Dev

布局水平锚点问题 - Swift 3

来自分类Dev

使用firebase的swift3中的时间戳问题

来自分类Dev

来自对象的 JSON 与 iOS Swift 3 中的数组问题

来自分类Dev

问题填充数组 swift 3

来自分类Dev

iOS Swift 3 发布问题

来自分类Dev

iOS Swift 3 POST 问题

来自分类Dev

这个 UIViewController 有什么问题?Swift 3,加载数据

来自分类Dev

Swift 3:用户触摸时出现 textView 光标问题

来自分类Dev

swift中范围问题的3d数组

来自分类Dev

Swift3:将失败块从 Swift2 转换为 Swift3 的 AFNetworking 问题

来自分类Dev

Swift 3 - iOS 11.2 上的 UICollectionview 问题