一張小的像素圖轉(zhuǎn)大圖時(shí)會(huì)變得模糊
圖片放大示例.png
iOS 像素圖轉(zhuǎn)高清大圖
有兩種方式可以使生成的大圖更清晰
- 用最近鄰插值算法生成大圖
- 按其寬高和像素行列去取顏色,CoreGraphics 用顏色數(shù)組畫多個(gè)矩形去繪制大圖
比較
隨機(jī)生成 20 張?jiān)瓐D 64px * 64px(iOS 由顏色數(shù)組生成像素圖)坏快,用上訴兩種方式生成大圖互捌,比對(duì)生成速度和內(nèi)存峰值:內(nèi)存用量幾乎完全一致需频,而速度上囚衔,放大倍數(shù)小時(shí)最近鄰插值更快憔古,放大倍數(shù)大時(shí) CoreGraphics 更快
因?yàn)樽罱彶逯邓惴ǖ脑硎钦业皆瓐D像中對(duì)應(yīng)的點(diǎn)后罢坝,找到離它最近的一個(gè)整數(shù)坐標(biāo)的像素值作為目標(biāo)圖像對(duì)應(yīng)位置處的像素值暑始,當(dāng)放大倍數(shù)越大搭独,處理次數(shù)越多
而 CoreGraphics 畫矩形,無論放大倍數(shù)多少廊镜,處理次數(shù)是一樣的
本文給出兩種方式的具體代碼牙肝,讀者可自行傳入倍數(shù),測(cè)試生成時(shí)間
最近鄰插值
CGInterpolationQuality 插值質(zhì)量
kCGInterpolationDefault
默認(rèn)
kCGInterpolationNone
無插值(最近鄰插值
kCGInterpolationLow
插值質(zhì)量低嗤朴,圖像渲染速度快
kCGInterpolationMedium
插值質(zhì)量中等配椭,渲染速度中等
kCGInterpolationHigh
插值質(zhì)量高,渲染速度較慢
extension UIImage {
func resize(to size: CGSize, interpolationQuality: CGInterpolationQuality = .none, isOpaque: Bool = false) -> UIImage? {
let format = imageRendererFormat
format.opaque = isOpaque
return UIGraphicsImageRenderer(size: size, format: format).image { context in
context.cgContext.interpolationQuality = interpolationQuality
draw(in: CGRect(origin: .zero, size: size))
}
}
}
CoreGraphics畫矩形
- 原圖轉(zhuǎn) UIColor 數(shù)組雹姊,注意行列非圖片本身寬高股缸,而是像素圖行列數(shù)(一格可能有很多像素)
- 用 UIColor 數(shù)組畫大圖,每個(gè) UIColor 對(duì)應(yīng)一個(gè)矩形吱雏,代碼在iOS 由顏色數(shù)組生成像素圖敦姻,看這段代碼可以理解圖片寬高和像素圖行列的關(guān)系(倍數(shù)關(guān)系)
extension UIImage {
/// 從 UIImage 轉(zhuǎn) colors
/// - Parameters:
/// - image: 圖
/// - column: 列(寬
/// - row: 行(高
static func colors(with image: UIImage, column: Int, row: Int) -> [UIColor]? {
guard let cgImage = image.cgImage else { return nil}
let imageRef = cgImage
let width = imageRef.width
let height = imageRef.height
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bytesPerPixel = 4
let bytesPerRow = bytesPerPixel * width
let bitsPerComponent = 8
let bitmapInfo: UInt32 = CGImageAlphaInfo.premultipliedLast.rawValue | CGBitmapInfo.byteOrder32Big.rawValue
guard let context = CGContext(data: nil, width: width, height: height,
bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow,
space: colorSpace, bitmapInfo: bitmapInfo),
let ptr = context.data?.assumingMemoryBound(to: UInt8.self) else {
return nil
}
context.draw(imageRef, in: CGRect(x: 0, y: 0, width: width, height: height))
var colors = [UIColor]()
let offset = width / column / 2
let perPixel = width / column
for y in 0..<row {
for x in 0..<column {
let i = (bytesPerRow * (y * perPixel + offset)) + (x * perPixel + offset) * bytesPerPixel
let alpha = CGFloat(ptr[i + 3]) / 255.0
let red = (CGFloat(ptr[i]) / alpha) / 255.0
let green = (CGFloat(ptr[i + 1]) / alpha) / 255.0
let blue = (CGFloat(ptr[i + 2]) / alpha) / 255.0
colors.append(UIColor(red: red, green: green, blue: blue, alpha: alpha))
}
}
return colors
}
}