mirror of
https://github.com/hyperoslo/Cache.git
synced 2026-04-07 19:17:36 +00:00
39 lines
651 B
Swift
39 lines
651 B
Swift
import UIKit
|
|
|
|
// MARK: - Cachable
|
|
|
|
extension UIImage: Cachable {
|
|
|
|
public typealias CacheType = UIImage
|
|
|
|
public static func decode(data: NSData) -> CacheType? {
|
|
let image = UIImage(data: data)
|
|
return image
|
|
}
|
|
|
|
public func encode() -> NSData? {
|
|
return hasAlpha
|
|
? UIImagePNGRepresentation(self)
|
|
: UIImageJPEGRepresentation(self, 1.0)
|
|
}
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
extension UIImage {
|
|
|
|
var hasAlpha: Bool {
|
|
let result: Bool
|
|
let alpha = CGImageGetAlphaInfo(CGImage)
|
|
|
|
switch alpha {
|
|
case .None, .NoneSkipFirst, .NoneSkipLast:
|
|
result = false
|
|
default:
|
|
result = true
|
|
}
|
|
|
|
return result
|
|
}
|
|
}
|