Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SwiftUI and MacOS support to BlurHashDecode #214

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 50 additions & 9 deletions Swift/BlurHashDecode.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,63 @@
#if canImport(SwiftUI)
import SwiftUI

extension Image {

#if canImport(UIKit)
public init?(blurHash: String, size: CGSize, punch: Float = 1) {
guard let uiImage = UIImage(blurHash: blurHash, size: size, punch: punch) else { return nil }
self.init(uiImage: uiImage)
}
#elseif canImport(AppKit)
public init?(blurHash: String, size: CGSize, punch: Float = 1) {
guard let nsImage = NSImage(blurHash: blurHash, size: size, punch: punch) else { return nil }
self.init(nsImage: nsImage)
}
#endif

}

#endif

#if canImport(UIKit)
import UIKit

extension UIImage {
public convenience init?(blurHash: String, size: CGSize, punch: Float = 1) {

guard let cgImage = CGImage.from(blurHash: blurHash, size: size, punch: punch) else { return nil }

self.init(cgImage: cgImage)
}
}
#elseif canImport(AppKit)
import AppKit

extension NSImage {
public convenience init?(blurHash: String, size: CGSize, punch: Float = 1) {
guard let cgImage = CGImage.from(blurHash: blurHash, size: size, punch: punch) else { return nil }

self.init(cgImage: cgImage, size: .zero)
}
}
#endif

extension CGImage {
static func from(blurHash: String, size: CGSize, punch: Float = 1) -> CGImage? {
kevin-hv marked this conversation as resolved.
Show resolved Hide resolved
guard blurHash.count >= 6 else { return nil }

let sizeFlag = String(blurHash[0]).decode83()
let numY = (sizeFlag / 9) + 1
let numX = (sizeFlag % 9) + 1
let sizeFlag = String(blurHash[0]).decode83()
let numY = (sizeFlag / 9) + 1
let numX = (sizeFlag % 9) + 1

let quantisedMaximumValue = String(blurHash[1]).decode83()
let quantisedMaximumValue = String(blurHash[1]).decode83()
let maximumValue = Float(quantisedMaximumValue + 1) / 166

guard blurHash.count == 4 + 2 * numX * numY else { return nil }

let colours: [(Float, Float, Float)] = (0 ..< numX * numY).map { i in
if i == 0 {
let value = String(blurHash[2 ..< 6]).decode83()
let value = String(blurHash[2 ..< 6]).decode83()
return decodeDC(value)
} else {
let value = String(blurHash[4 + i * 2 ..< 4 + i * 2 + 2]).decode83()
Expand Down Expand Up @@ -59,10 +101,9 @@ extension UIImage {
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue)

guard let provider = CGDataProvider(data: data) else { return nil }
guard let cgImage = CGImage(width: width, height: height, bitsPerComponent: 8, bitsPerPixel: 24, bytesPerRow: bytesPerRow,
space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: bitmapInfo, provider: provider, decode: nil, shouldInterpolate: true, intent: .defaultIntent) else { return nil }

self.init(cgImage: cgImage)

return CGImage(width: width, height: height, bitsPerComponent: 8, bitsPerPixel: 24, bytesPerRow: bytesPerRow,
space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: bitmapInfo, provider: provider, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
}
}

Expand Down