ImageIO In Swift
//可读取的文件格式
let sourceSupportTypes = CGImageSourceCopyTypeIdentifiers()
//可写入的文件格式
let destinationSupportTypes = CGImageDestinationCopyTypeIdentifiers()
CGImageSource
图片上下文,解析出图片数据(图片宽高,颜色空间,图片大小...)
通过CGImageSourceCreate创建
guard let cgImageSource = CGImageSourceCreateWithURL(url, options) else {
print("Create Image Source failure.")
return
}
CGImageSourceGetCount 可以获取图片上下文中解析出来的图片数量
通过CGImageSourceCreateImageAtIndex 可以获取已解码的图片数据CGImage
CGImageSourceCreateThumbnailAtIndex可以从上下文中读取到缩略图
options中的kCGImageSourceCreateThumbnailFromImageIfAbsent如果不指定的话可能会读取不到缩略图,指定为true,表示是否自动创建缩略图。当上下文中没有包含有缩略图数据时,为true就会根据原始图生成缩略图。
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
}
如果遇到大的图片,比如在读取网络图片时可以使用CGImageSourceCreateIncremental来渐进加载图片,步骤如下:
1.创建一个CFMutableData
2.创建一个Incremental Image Source
3.向CFMutableData中添加已经接受到的Data数据
4.调用CGImageSourceUpdateData来将数据更新到Incremental Image Source
5.读取图片状态CGImageSourceGetStatus,判断状态是否为status complete
6.通过CGImageSourceCreateImageAtIndex或CGImageSourceCreateThumbnailAtIndex读取图片数据
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