扩展属性
base64
说明:base64字符串
示例
print(#imageLiteral(resourceName: "mallSearchIcon").base64)
输出结果:略
byteSize
说明:大小,单位byte
示例
print(#imageLiteral(resourceName: "mallSearchIcon").byteSize)
输出结果:6080
kilobyteSize
说明:大小,单位kb
示例
print(#imageLiteral(resourceName: "mallSearchIcon").kilobyteSize)
输出结果:5
构造方法
public convenience init?(_ urlString: String)
说明:通过url构造
示例
UIImage("http://pic35.nipic.com/20131121/2531170_145358633000_2.jpg")
输出结果:
public convenience init(_ color: UIColor)
说明:通过UIColor构造
示例
UIImage(UIColor.red)
输出结果:
public convenience init(size:CGSize,colors: [UIColor],locations: [CGFloat],vector: CGVector = CGVector.init(dx: 1, dy: 0))
说明:构造渐变Image
- size: 大小
- colors: 颜色数组
- locations: 位置数组(0~1之间)
- vector: 方向向量,默认从左到右
示例
UIImage(size:barcode.bounds.size ,colors:[UIColor.red,UIColor.yellow,UIColor.blue],locations:[0,0.5,1])
输出结果:
UIImage(size:barcode.bounds.size ,colors:[UIColor.red,UIColor.yellow,UIColor.green,UIColor.blue],locations:[0,0.4,0.6,1],vector:CGVector(dx: 1, dy: 1))
输出结果:
常用方法
public func scale(to: CGSize) -> UIImage
说明:缩放到指定尺寸
示例
print(#imageLiteral(resourceName: "mallSearchIcon"))
print(#imageLiteral(resourceName: "mallSearchIcon").scale(to: CGSize(width:50,height:50)))
输出结果:
<UIImage: 0x6000000a4aa0>, {26, 26}
<UIImage: 0x60c0000a6780>, {50, 50}
public func changeColor(_ tintColor: UIColor) -> UIImage?
说明:修改颜色
示例
self.resultImgV.image = #imageLiteral(resourceName: "mallSearchIcon").changeColor(UIColor.red)
输出结果:
public func saveToAlbum(_ result: ((_ isSuc: Bool)->Void)?)
说明: 保存到相册
示例
self.resultImgV.image.saveToAlbum { (isSuc) in
print(isSuc)
}
输出结果:true
public func similarityValue(_ with: UIImage,_ imageSize: Int = 10) -> Double
说明:图片相似度(AR红包)
public func color(_ at: CGPoint) -> UIColor?
说明:指定像素点的颜色
类方法
public class func blankImage() -> UIImage
说明:空白图