https://github.com/dmytro-anokhin/image-decoder
Swift image decoder using Image I/O
https://github.com/dmytro-anokhin/image-decoder
image swift swiftui uikit
Last synced: 2 months ago
JSON representation
Swift image decoder using Image I/O
- Host: GitHub
- URL: https://github.com/dmytro-anokhin/image-decoder
- Owner: dmytro-anokhin
- License: other
- Created: 2019-11-17T20:56:06.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-11-20T04:10:13.000Z (over 5 years ago)
- Last Synced: 2025-03-26T21:47:02.942Z (3 months ago)
- Topics: image, swift, swiftui, uikit
- Language: Roff
- Size: 30.3 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APPLE
Awesome Lists containing this project
README
# ImageDecoder
Image decoder in Swift using Image I/O. This implementation is based on WebKit (WebCore `ImageDecoderCG` class) and you can expect similar to how Safari handles images.
This pacakge is handy if you need to:
- Incrementally load an image;
- Decode animated images.**ImageDecoder** supports animated images in GIF, APNG, and HEICS formats.
## Usage
Use [Swift Package Manager integration in Xcode](https://developer.apple.com/documentation/xcode/adding_package_dependencies_to_your_app) to install **ImageDecoder**.
If you have the complete image data you can create `ImageDecoder` object and set it, `allDataReceived` indicates if the image data is complete:
```swift
let imageDecoder = ImageDecoder()
imageDecoder.setData(data, allDataReceived: true)
```Use this approach if you read the image data from a file or downloaded from network using `URLSessionDataTask`:
```swift
let task = urlSession.dataTask(with: url) { data, _, _ in
guard let data = data else {
return
}
let imageDecoder = ImageDecoder()
imageDecoder.setData(data, allDataReceived: true)
guard let uiImage = imageDecoder.uiImage else {
return
}
DispatchQueue.main.async {
self.uiImage = uiImage
}
}
task.resume()
```When you incrementally loading an image create the image data object for accumulating the image data. Pass the partial image data to `ImageDecoder` and the complete image data when loading completes. This example uses `URLSessionDelegate`:
```swift
let imageDecoder = ImageDecoder()var imageData = Data()
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
imageData.append(data)
imageDecoder.setData(imageData, allDataReceived: false)
}func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
imageDecoder.setData(imageData, allDataReceived: true)
}
```## Decoding an image
`ImageDecoder` can create animated or static images. Use `createFrameImage(at index: Int, subsamplingLevel: SubsamplingLevel = .default, decodingOptions: DecodingOptions = .default) -> CGImage?`.
Creating static image:
```swift
let cgImage = imageDecoder.createFrameImage(at: 0)
```Animated image has multiple frames:
```swift
for i in 0..) {
uiView.image = image
}
func makeUIView(context: UIViewRepresentableContext) -> UIImageView {
let imageView = UIImageView(image: image)
imageView.startAnimating()
return imageView
}
}
```## Performance
Decoding an animated image can sometimes be slow because each frame must be decoded. Use background queue to decode animated image and make sure you use `DecodingOptions.Mode.asynchronous`. This is the default decoding mode. If you decode images on the main queue use `DecodingOptions.Mode.synchronous`.
## Misc
If you found a bug, have a feature request, or want to contribute - please open an issue.
Make sure to check out [URLImage](https://github.com/dmytro-anokhin/url-image) package if you need to dowload and display an image in SwiftUI. For updates on this and other packages follow me on Twitter: [dmytroanokhin](https://twitter.com/dmytroanokhin).