地址:http://www.hangge.com/blog/cache/detail_975.html
一,ImageHelper介紹
ImageHelper(原來叫AFImageHelper)是使用Swift語言編寫的處理圖片的類庫伐割,通過對 UIImage 和 UIImageView 的擴(kuò)展做鹰。使其增加了對圖片的壓縮、顏色捉腥、漸變换况、裁剪等操作方法琅摩,以及支持使用緩存從網(wǎng)站上獲取圖片此迅。
二汽畴,ImageHelper的配置
(1)從GitHub上把最新代碼下載下來,地址:https://github.com/melvitax/ImageHelper
(2)將 ImageHelper.swift耸序、ImageVIewExtension.swift 添加到項(xiàng)目中即可
原文:Swift - 圖片處理庫ImageHelper詳解(擴(kuò)展UIImage,UIImageView)
三忍些,UIImageView擴(kuò)展相關(guān)的使用樣例
1,讓UIImageView支持直接通過URL地址獲取圖片
(1)可以設(shè)置是否需要緩存圖片(默認(rèn)是緩存)坎怪。是的話罢坝,每次網(wǎng)絡(luò)請求時(shí),會先自動判斷下是否本地有緩存圖片搅窿,如果有則直接使用緩存圖片嘁酿。對于新加載下來的圖片則會緩存起來,供下次使用戈钢。
原文:Swift - 圖片處理庫ImageHelper詳解(擴(kuò)展UIImage,UIImageView)
1
imageView1.imageFromURL("http://www.hangge.com/blog/images/logo.png", placeholder: UIImage())
(2)支持設(shè)置占位符圖片(placeholder)痹仙,在網(wǎng)絡(luò)圖片未加載完畢的時(shí)候顯示是尔。
原文:Swift - 圖片處理庫ImageHelper詳解(擴(kuò)展UIImage,UIImageView)
1
2
imageView1.imageFromURL("http://www.hangge.com/blog/images/logo.png",
placeholder: UIImage(named: "loading")!)
(3)還可以設(shè)置圖片加載完畢后是否需要淡入顯示(默認(rèn)是true)殉了。
1
2
3
imageView1.imageFromURL("http://www.hangge.com/blog/images/logo.png",
placeholder: UIImage(named: "loading")!,
fadeIn: true)
(4)可以在圖片加載成功的回調(diào)函數(shù)中做一些后續(xù)處理。
1
2
3
4
5
6
7
imageView1.imageFromURL("http://www.hangge.com/blog/images/logo.png",
placeholder: UIImage(named: "loading")!, fadeIn: true, shouldCacheImage: true) {
(image: UIImage?) in
if image != nil {
print("圖片加載成功!")
}
}
四拟枚,UIImage擴(kuò)展相關(guān)的使用樣例
1薪铜,通過URL地址獲取圖片
同 UIImageView 一樣,UIImage 也可以通過URL獲取網(wǎng)絡(luò)圖片恩溅。同樣支持占位符圖片隔箍,圖片緩存,加載完畢回調(diào)脚乡。
(其實(shí)UIImageView的url圖片加載內(nèi)部調(diào)用的就是 UIImage.image(fromURL: url) 方法)
1
2
3
4
5
6
7
let url = "http://www.hangge.com/blog/images/logo.png"
UIImage.image(fromURL: url, placeholder: UIImage(), shouldCacheImage: true) {
(image: UIImage?) in
if image != nil {
self.imageView1.image = image
}
}
2蜒滩,通過顏色生成UIImage
(1)使用純色
原文:Swift - 圖片處理庫ImageHelper詳解(擴(kuò)展UIImage,UIImageView)
1
UIImage(color: UIColor.orange, size: CGSize(width: 55, height: 30))
(2)使用線性漸變色
原文:Swift - 圖片處理庫ImageHelper詳解(擴(kuò)展UIImage,UIImageView)
1
2
let gradientColors = [UIColor.orange, UIColor.red]
UIImage(gradientColors: gradientColors, size: CGSize(width: 55, height: 30))
(3)使用放射性漸變色
原文:Swift - 圖片處理庫ImageHelper詳解(擴(kuò)展UIImage,UIImageView)
1
2
3
UIImage(startColor: UIColor.orange, endColor: UIColor.red,
radialGradientCenter: CGPoint(x: 0.5, y: 0.5), radius: 1,
size:? CGSize(width: 55, height: 30))
3,給圖片覆蓋一層漸變色
下面給原始的UIImage添加一個半透明的黃褐色漸變奶稠,使圖片有種添加老照片濾鏡的效果俯艰。
(默認(rèn)的混合模式是 CGBlendMode.Normal,也可以設(shè)置成其他的 blendMode)
原文:Swift - 圖片處理庫ImageHelper詳解(擴(kuò)展UIImage,UIImageView)
1
2
3
let gradientColors = [UIColor(red: 0.996, green: 0.769, blue: 0.494, alpha: 1.0),
UIColor(red: 0.969, green: 0.608, blue: 0.212, alpha: 0.2)]
imageView2.image = UIImage(named: "beach")?.apply(gradientColors: gradientColors)
4锌订,通過Text文本生成UIImage
除了設(shè)置文字內(nèi)容竹握,還可以設(shè)置文字大小,文字顏色和背景顏色辆飘。
原文:Swift - 圖片處理庫ImageHelper詳解(擴(kuò)展UIImage,UIImageView)
1
2
3
4
5
6
7
8
9
10
//假設(shè)界面上的UIImageView尺寸是180*80
let textSize = 46 * UIScreen.main.scale
let imageWidth = 180 * UIScreen.main.scale
let imageHeight = 80 * UIScreen.main.scale
if let image = UIImage(text: "hangge", font: UIFont.systemFont(ofSize: textSize),
color: UIColor.white, backgroundColor: UIColor.orange,
size: CGSize(width: imageWidth, height: imageHeight)){
imageView1.image = image
}
5啦辐,對任意UIView對象截圖(Screenshot)
1
2
3
//將當(dāng)前頁面轉(zhuǎn)換成image
let image = UIImage(fromView: self.view)
imageView2.image = image
6谓传,透明層相關(guān)(alpha layer)
1
2
3
4
5
//判斷圖片是否有透明圖層
UIImage(named: "logo")?.hasAlpha
//給圖片添加透明涂層
UIImage(named: "logo")?.applyAlpha()
7,給圖片增加外邊距(添加透明邊框)
下面樣例上下兩個imageView的尺寸是一樣的芹关,樣式都是Aspect Fit续挟。下面一個使用的image添加了透明邊距。
原文:Swift - 圖片處理庫ImageHelper詳解(擴(kuò)展UIImage,UIImageView)
1
2
imageView1.image = UIImage(named: "beach")
imageView2.image = UIImage(named: "beach")?.apply(padding: 50)
8侥衬,圖片裁剪
(1)自定義裁剪位置和尺寸
原文:Swift - 圖片處理庫ImageHelper詳解(擴(kuò)展UIImage,UIImageView)
1
2
3
4
imageView1.image = UIImage(named: "beach")
let rect = CGRect(x: 0, y: 0, width: 500, height: 200)
imageView2.image = UIImage(named: "beach")?.crop(bounds: rect)
(2)自動裁剪成正方形
原文:Swift - 圖片處理庫ImageHelper詳解(擴(kuò)展UIImage,UIImageView)
1
2
imageView1.image = UIImage(named: "beach")
imageView2.image = UIImage(named: "beach")?.cropToSquare()
9庸推,調(diào)整尺寸大小
原文:Swift - 圖片處理庫ImageHelper詳解(擴(kuò)展UIImage,UIImageView)
1
2
imageView1.image = UIImage(named: "beach")
imageView2.image = UIImage(named: "beach")?.resize(toSize: CGSize(width: 300, height: 400))
由于不同設(shè)備的縮放比例不同,可以通過 screen scale 乘以固定的寬高的辦法動態(tài)設(shè)置尺寸浇冰,保證各種設(shè)備下顯示正常贬媒,不會模糊。(對于padding肘习、borders的設(shè)置也可以用此方法)
1
2
3
let width = 300 * UIScreen.main.scale
let height = 400 * UIScreen.main.scale
let image = UIImage(named: "myImage")?.resize(toSize: CGSize(width: width, height: height))
10际乘,生成圓角或圓形圖片
(1)圓角圖片
原文:Swift - 圖片處理庫ImageHelper詳解(擴(kuò)展UIImage,UIImageView)
1
imageView2.image = UIImage(named: "beach")?.roundCorners(cornerRadius: 70)
(2)帶邊框的圓角圖片
原文:Swift - 圖片處理庫ImageHelper詳解(擴(kuò)展UIImage,UIImageView)
1
2
imageView1.image = UIImage(named: "beach")?.roundCorners(cornerRadius: 70, border: 200,
color: UIColor.orange)
(3)圓形圖片
原文:Swift - 圖片處理庫ImageHelper詳解(擴(kuò)展UIImage,UIImageView)
1
imageView2.image = UIImage(named: "beach")?.roundCornersToCircle()
(4)帶邊框的圓形圖片
原文:Swift - 圖片處理庫ImageHelper詳解(擴(kuò)展UIImage,UIImageView)
1
2
imageView2.image = UIImage(named: "beach")?.roundCornersToCircle(withBorder: 40,
color: UIColor.orange)
原文出自:www.hangge.com? 轉(zhuǎn)載請保留原文鏈接:http://www.hangge.com/blog/cache/detail_975.html