//
|
// 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!
|
}
|
}
|