宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//
//  UIImageExtension.swift
//  THProject
//
//  Created by alvin_y on 2018/4/8.
//  Copyright © 2018年 yang-wang. All rights reserved.
//
 
import UIKit
import Photos
 
public extension UIImage {
    
    // 渐变方式
    enum GradientType {
        case topToBottom // 上到下
        case leftToRight // 左到右
        case upleftTolowRight // 左上到右下
        case uprightTolowLeft // 右上到左下
    }
    
    enum ComposeType {
        case center
        case top // 上
        case bottom // 下
    }
    
    //
    static func screenScale() -> CGFloat {
        return UIScreen.main.scale
    }
    
    //MARK: - 初始化一个有颜色的
    convenience init?(color: UIColor) {
        let rect: CGRect = CGRect(x: 0, y: 0, width: 1 * UIImage.screenScale(), height: 1 * UIImage.screenScale())
        UIGraphicsBeginImageContext(rect.size)
        
        color.setFill()
        UIRectFill(rect)
        
        guard let image: UIImage = UIGraphicsGetImageFromCurrentImageContext() else {
            UIGraphicsEndImageContext()
            return nil
        }
        UIGraphicsEndImageContext()
        
        self.init(cgImage: image.cgImage!, scale: UIImage.screenScale(), orientation: .up)
    }
 
    /// PHAsset转UIImage
    @objc static func JQ_PHAssetToImage(asset:PHAsset) -> UIImage{
        var image = UIImage()
 
        // 新建一个默认类型的图像管理器imageManager
        let imageManager = PHImageManager.default()
 
        // 新建一个PHImageRequestOptions对象
        let imageRequestOption = PHImageRequestOptions()
 
        // PHImageRequestOptions是否有效
        imageRequestOption.isSynchronous = true
 
        // 缩略图的压缩模式设置为无
        imageRequestOption.resizeMode = .none
 
        // 缩略图的质量为高质量,不管加载时间花多少
        imageRequestOption.deliveryMode = .highQualityFormat
 
        // 按照PHImageRequestOptions指定的规则取出图片
        imageManager.requestImage(for: asset, targetSize: CGSize.init(width: 1080, height: 1920), contentMode: .aspectFill, options: imageRequestOption, resultHandler: {
            (result, _) -> Void in
            image = result!
        })
        return image
    }
    
    //MARK: - 图片旋转 radians
    
    ///  图片旋转
    /// - Parameter radians: rotationAngle
    func rotate(radians: Float) -> UIImage {
        let degrees = Float(Double(radians) * 180 / Double.pi)
        let rotatedViewBox: UIView = UIView(frame: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
        let rotationAngle = CGFloat(Double(degrees) * Double.pi / 180)
        let transformation: CGAffineTransform = CGAffineTransform(rotationAngle: rotationAngle)
        rotatedViewBox.transform = transformation
        let rotatedSize: CGSize = CGSize(width: Int(rotatedViewBox.frame.size.width), height: Int(rotatedViewBox.frame.size.height))
        
        UIGraphicsBeginImageContextWithOptions(rotatedSize, false, UIImage.screenScale())
        guard let context: CGContext = UIGraphicsGetCurrentContext() else {
            UIGraphicsEndImageContext()
            return self
        }
        
        context.translateBy(x: rotatedSize.width / 2, y: rotatedSize.height / 2)
        
        context.rotate(by: rotationAngle)
        
        context.scaleBy(x: 1.0, y: -1.0)
        context.draw(self.cgImage!, in: CGRect(x: -self.size.width / 2, y: -self.size.height / 2, width: self.size.width, height: self.size.height))
        
        guard let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() else {
            UIGraphicsEndImageContext()
            return self
        }
        UIGraphicsEndImageContext()
        
        return newImage
    }
    
    //MARK: - 创建一个渐变的图片
    
    /// 创建一个渐变的图片
    /// - Parameters:
    ///   - size: CGSize
    ///   - colors: [UIColor]
    ///   - gradientType: GradientType
    class func yy_creat(size:CGSize, colors:[UIColor]?, gradientType:GradientType) -> UIImage? {
        
        guard let aColors = colors else {
            return nil
        }
        
        UIGraphicsBeginImageContextWithOptions(size, true, screenScale())
        let context = UIGraphicsGetCurrentContext()
        context?.saveGState()
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let otherColors = aColors.map { $0.cgColor } as CFArray
        let gradient = CGGradient.init(colorsSpace: colorSpace, colors: otherColors, locations: [0,1])
        
        var start = CGPoint(x: 0.0, y: 0.0)
        var end = CGPoint(x: 0.0, y: 0.0)
        
        switch gradientType {
        case .leftToRight:
            start = CGPoint(x: 0.0, y: 0.0)
            end = CGPoint(x: size.width, y: 0.0)
            break
        case .topToBottom:
            start = CGPoint(x: 0.0, y: 0.0)
            end = CGPoint(x: 0.0, y: size.height)
            break
        case .upleftTolowRight:
            start = CGPoint(x: 0.0, y: 0.0)
            end = CGPoint(x: size.width, y: size.height)
            break
        case .uprightTolowLeft:
            start = CGPoint(x: size.width, y: 0.0)
            end = CGPoint(x: 0.0, y: size.height)
            break
        }
        
        guard let aGradient = gradient  else {
            return nil
        }
        context?.drawLinearGradient(aGradient, start: start, end: end, options: [.drawsBeforeStartLocation,.drawsAfterEndLocation])
        let image = UIGraphicsGetImageFromCurrentImageContext()
        context?.restoreGState()
        UIGraphicsEndImageContext()
        return image
    }
    
    //MARK: - 合成图片
    
    /// 合成图片
    /// - Parameters:
    ///   - bottomImage: 底部图片
    ///   - upImage: 顶部图片
    ///   - type: ComposeType
    static func yy_composeImage(bottomImage:UIImage, upImage:UIImage, type:ComposeType = ComposeType.center) -> UIImage? {
        
        guard let imgRef = upImage.cgImage else {
            return nil
        }
        
        let w = CGFloat(imgRef.width)
        let h = CGFloat(imgRef.height)
        
        guard let imgRef1 = bottomImage.cgImage else {
            return nil
        }
        
        let w1 = CGFloat(imgRef1.width)
        let h1 = CGFloat(imgRef1.height)
        
        UIGraphicsBeginImageContext(CGSize.init(width: w1, height: h1))
        bottomImage.draw(in: CGRect.init(x: 0, y: 0, width: w1, height: h1))
        if type == .center {
            upImage.draw(in: CGRect.init(x: (w1-w)/2.0, y: (h1-h)/2.0, width: w, height: h))
        } else if type == .top {
            upImage.draw(in: CGRect.init(x: 0.0, y: 0.0, width: w, height: h))
        } else {
            upImage.draw(in: CGRect.init(x: 0.0, y: h1-h, width: w, height: h))
        }
        
        
        guard let resultImg = UIGraphicsGetImageFromCurrentImageContext() else {
            UIGraphicsEndImageContext()
            return nil
        }
        UIGraphicsEndImageContext()
        return resultImg
        
    }
    
    //MARK: - 把视图转成图片
    
    /// 把视图转成图片
    /// - Parameter view: UIView
    class func getImageFromView(view: UIView) ->UIImage? {
        
        //        UIGraphicsBeginImageContext(view.bounds.size)
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.main.scale)
        guard let context = UIGraphicsGetCurrentContext() else {
            return nil
        }
        view.layer.render(in: context)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
    
    //MARK: - 图片重绘
    
    /// 图片重绘
    /// - Parameters:
    ///   - image: UIImage
    ///   - size: CGSize
    class func yy_image(image:UIImage,size:CGSize) -> UIImage? {
        
        UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
        image.draw(in: CGRect.init(x: 0, y: 0, width: size.width, height: size.height))
        
        guard let newImage = UIGraphicsGetImageFromCurrentImageContext() else {
            return nil
        }
        
        UIGraphicsEndImageContext()
        
        return newImage
    }
 
    
    /// 文字合成图片
    /// - Parameters:
    ///   - text: 文字
    ///   - size: size
    ///   - backColor: backColor
    ///   - textColor: textColor
    ///   - isCircle: isCircle
    func image(_ text:String,size:(CGFloat,CGFloat),backColor:UIColor=UIColor.orange,textColor:UIColor=UIColor.white,isCircle:Bool=true) -> UIImage?{
        // 过滤空""
        if text.isEmpty { return nil }
        // 取第一个字符(测试了,太长了的话,效果并不好)
        let letter = (text as NSString).substring(to: 1)
        let sise = CGSize(width: size.0, height: size.1)
        let rect = CGRect(origin: CGPoint.zero, size: sise)
        // 开启上下文
        UIGraphicsBeginImageContext(sise)
        // 拿到上下文
        guard let ctx = UIGraphicsGetCurrentContext() else { return nil }
        // 取较小的边
        let minSide = min(size.0, size.1)
        // 是否圆角裁剪
        if isCircle {
            UIBezierPath(roundedRect: rect, cornerRadius: minSide*0.5).addClip()
        }
        // 设置填充颜色
        ctx.setFillColor(backColor.cgColor)
        // 填充绘制
        ctx.fill(rect)
        let attr = [NSAttributedString.Key.foregroundColor : textColor, NSAttributedString.Key.font : UIFont.systemFont(ofSize: minSide*0.5)]
        // 写入文字
        (letter as NSString).draw(at: CGPoint(x: minSide*0.25, y: minSide*0.25), withAttributes: attr)
        // 得到图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        // 关闭上下文
        UIGraphicsEndImageContext()
        return image
    }
    
    //MARK: - 更改图片颜色
    
    /// 更改图片颜色
    /// - Parameter color: UIColor
    func imageWithTintColor(color : UIColor) -> UIImage{
        UIGraphicsBeginImageContext(self.size)
        color.setFill()
        let bounds = CGRect.init(x: 0, y: 0, width: self.size.width, height: self.size.height)
        UIRectFill(bounds)
        self.draw(in: bounds, blendMode: CGBlendMode.destinationIn, alpha: 1.0)
        
        let tintedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return tintedImage!
    }
}