Files
hyperoslo-Cache/Source/iOS/UIImage+Extensions.swift
T
Rizwan 3ccd88ad75 Made the code compatible with Swift 4.2 by doing the following,
Replaced the method UIImagePNGRepresentation(self) with self.pngData()
Replaced the method UIImageJPEGRepresentation(self, 1.0) with self.jpegData(compressionQuality: 1.0)
2018-09-19 22:15:10 +05:30

30 lines
526 B
Swift

import UIKit
/// Helper UIImage extension.
extension UIImage {
/// Checks if image has alpha component
var hasAlpha: Bool {
let result: Bool
guard let alpha = cgImage?.alphaInfo else {
return false
}
switch alpha {
case .none, .noneSkipFirst, .noneSkipLast:
result = false
default:
result = true
}
return result
}
/// Convert to data
func cache_toData() -> Data? {
return hasAlpha
? self.pngData()
: self.jpegData(compressionQuality: 1.0)
}
}