先放demo地址:github.com/KeWangKW/99…
(一)UIButton拓展【修改图文排列】
- 代码文件: UIButtonExt
- 示例文件: UIButtonExtShow
import UIKit
public extension UIButton {
@objc enum Position:NSInteger {
case top, left, bottom, right
}
//MARK: 文字图片布局
/// 文字图片布局
@objc func kw_set(image position: Position, spacing: CGFloat) {
self.layoutIfNeeded()
guard
let imageWidth = self.imageView?.frame.width,
let imageHeight = self.imageView?.frame.height,
let labelWidth = self.titleLabel?.frame.width,
let labelHeight = self.titleLabel?.frame.height
else { return }
let space = spacing
var imageInsets = UIEdgeInsets.zero
var labelInsets = UIEdgeInsets.zero
switch position {
case .top:
imageInsets = UIEdgeInsets(top: -labelHeight - space/2.0, left: 0, bottom: 0, right: -labelWidth)
labelInsets = UIEdgeInsets(top: 0, left: -imageWidth, bottom: -imageHeight - space/2.0, right: 0)
break
case .left:
imageInsets = UIEdgeInsets(top: 0, left: -space/2.0, bottom: 0, right: space/2.0)
labelInsets = UIEdgeInsets(top: 0, left: space/2.0, bottom: 0, right: -space/2.0)
break
case .bottom:
imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: -labelHeight - space/2.0, right: -labelWidth)
labelInsets = UIEdgeInsets(top: -imageHeight - space/2.0, left: -imageWidth, bottom: 0, right: 0)
break
case .right:
imageInsets = UIEdgeInsets(top: 0, left: labelWidth + space/2.0, bottom: 0, right: -labelWidth - space/2.0)
labelInsets = UIEdgeInsets(top: 0, left: -imageWidth - space/2.0, bottom: 0, right: imageWidth + space/2.0)
break
}
self.imageEdgeInsets = imageInsets
self.titleEdgeInsets = labelInsets
}
}
-
默认UIButton 图片在左侧,文字在右侧,图文之间无间距
-
设置:图片在左侧,文字在右侧,间距为10
- btn1.kw_set(image: .left, spacing: 10)
- [btn1 kw_setWithImage:PositionLeft spacing:10];
-
设置:图片在右侧,文字在左侧,间距为10
- btn2.kw_set(image: .right, spacing: 10)
- [btn2 kw_setWithImage:PositionRight spacing:10];
-
设置:图片在上侧,文字在下侧,间距为10
- btn3.kw_set(image: .top, spacing: 10)
- [btn3 kw_setWithImage:PositionTop spacing:10];
-
设置:图片在下侧,文字在上侧,间距为10
- btn4.kw_set(image: .bottom, spacing: 10)
- [btn4 kw_setWithImage:PositionBottom spacing:10];
===========================================================================
(二)UIImage拓展【重写图片颜色、 TZImagePickerController 使用 、图片压缩 、视频压缩】
1. 修改图标颜色
-
ios13后可使用
open func withTintColor(_ color: UIColor) -> UIImage方法直接修改 -
ios13前需要自行绘制新图片
-
UIImage(named: "icon_gongzuotai02")!.kw_tintColor(.red)
-
[[UIImage imageNamed:@"icon_gongzuotai02"] kw_tintColor:[UIColor redColor]]
2. 图片压缩
- 一般可使用
public func jpegData(compressionQuality: CGFloat) -> Data?系统方法进行压缩 - 某些情况下,例如上传时需要把图片压缩到指定大小
- data = UIImage(named: "233")!.kw_compressedToData(maxSize: 100)
- 将 图片压缩到 100kb 以内
3. 视频压缩
- 调用 UIImageExt 文件中的 kw_compressedVideoToUrlStr 方法,返回视频的url地址、视频尺寸、时长【测试能将25M的视频压缩至2M】
-
- TZImagePickerController 选择视频
- swift在使用 TZImagePickerController 时,block回调代码不提示,需手动填写
- tz.didFinishPickingVideoHandle = {
coverImage,asset in}
-
- 选择视频回调中,调用方法
- UIImage.kw_compressedVideoToUrlStr(asset!) { urlStr ,size ,duration in }
-
- 处理上传操作,操作完成后需要删除文件
- UIImage.removeFile(filePath: urlStr!)