ImageIO In Swift
//可讀取的文件格式
let sourceSupportTypes = CGImageSourceCopyTypeIdentifiers()
//可寫入的文件格式
let destinationSupportTypes = CGImageDestinationCopyTypeIdentifiers()
CGImageSource
圖片上下文,解析出圖片數(shù)據(jù)(圖片寬高,顏色空間,圖片大小...)
通過CGImageSourceCreate創(chuàng)建
guard let cgImageSource = CGImageSourceCreateWithURL(url, options) else {
print("Create Image Source failure.")
return
}
CGImageSourceGetCount 可以獲取圖片上下文中解析出來的圖片數(shù)量
通過CGImageSourceCreateImageAtIndex 可以獲取已解碼的圖片數(shù)據(jù)CGImage
CGImageSourceCreateThumbnailAtIndex可以從上下文中讀取到縮略圖
options中的kCGImageSourceCreateThumbnailFromImageIfAbsent如果不指定的話可能會讀取不到縮略圖,指定為true,表示是否自動創(chuàng)建縮略圖。當(dāng)上下文中沒有包含有縮略圖數(shù)據(jù)時,為true就會根據(jù)原始圖生成縮略圖。
kCGImageSourceThumbnailMaxPixelSize:指定了縮略圖的最大寬高都不能超過這個值
var size = cgImage.height / 2
let thumbSizeNumber = CFNumberCreate(CFAllocatorGetDefault().takeUnretainedValue(), CFNumberType.intType, &size)
let thumb_options = CFDictionaryCreateMutable(CFAllocatorGetDefault().takeUnretainedValue(), 0, nil, nil)
CFDictionaryAddValue(thumb_options, Unmanaged.passUnretained(kCGImageSourceThumbnailMaxPixelSize).toOpaque(), Unmanaged.passUnretained(thumbSizeNumber!).toOpaque())
CFDictionaryAddValue(thumb_options, Unmanaged.passUnretained(kCGImageSourceCreateThumbnailFromImageIfAbsent).toOpaque(), Unmanaged.passUnretained(kCFBooleanTrue).toOpaque())
guard let cgThumbImage = CGImageSourceCreateThumbnailAtIndex(cgImageSource, 0, thumb_options) else {
print("Create Thumb CGImage failure.")
return
}
如果遇到大的圖片,比如在讀取網(wǎng)絡(luò)圖片時可以使用CGImageSourceCreateIncremental來漸進(jìn)加載圖片,步驟如下:
1.創(chuàng)建一個CFMutableData
2.創(chuàng)建一個Incremental Image Source
3.向CFMutableData中添加已經(jīng)接受到的Data數(shù)據(jù)
4.調(diào)用CGImageSourceUpdateData來將數(shù)據(jù)更新到Incremental Image Source
5.讀取圖片狀態(tài)CGImageSourceGetStatus,判斷狀態(tài)是否為status complete
6.通過CGImageSourceCreateImageAtIndex或CGImageSourceCreateThumbnailAtIndex讀取圖片數(shù)據(jù)
guard let incrementData = CFDataCreateMutable(CFAllocatorGetDefault().takeUnretainedValue(), 0) else {
fatalError()
}
let thumb_options = CFDictionaryCreateMutable(CFAllocatorGetDefault().takeUnretainedValue(), 0, nil, nil)
CFDictionaryAddValue(thumb_options, Unmanaged.passUnretained(kCGImageSourceCreateThumbnailFromImageIfAbsent).toOpaque(), Unmanaged.passUnretained(kCFBooleanTrue).toOpaque())
CFDictionaryAddValue(thumb_options, Unmanaged.passUnretained(kCGImageSourceCreateThumbnailWithTransform).toOpaque(), Unmanaged.passUnretained(kCFBooleanTrue).toOpaque())
DispatchQueue.global().async {
let incrementSource = CGImageSourceCreateIncremental(nil)
let byteCount = Int(percent * Float(self.fullData.count))
print(byteCount, self.fullData.count)
let bytes = UnsafeMutablePointer<UInt8>.allocate(capacity: byteCount)
self.fullData.copyBytes(to: bytes, count: byteCount)
CFDataAppendBytes(incrementData, bytes, byteCount)
CGImageSourceUpdateData(incrementSource, incrementData, true)
let status = CGImageSourceGetStatus(incrementSource)
switch status {
case .statusComplete:
if let cgImage = CGImageSourceCreateThumbnailAtIndex(incrementSource, 0, thumb_options) {
DispatchQueue.main.async {
self.imageView.image = UIImage(cgImage: cgImage)
}
}
default:
break
}
bytes.deallocate()
}
更多接口:https://developer.apple.com/documentation/imageio/cgimagesource-r84