Files
hyperoslo-Cache/Source/iOS/Extensions/UIImage+Cache.swift
T
2015-12-21 13:04:03 +01:00

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
}
}