Skip to content

Instantly share code, notes, and snippets.

View dmytro-anokhin's full-sized avatar
🔨

Dmytro Anokhin dmytro-anokhin

🔨
  • London
View GitHub Profile
enum SubsamplingLevel: Int {
case level0 = 1
case level1 = 2
case level2 = 4
case level3 = 8
private static let imageSourceOptions: [CFString: Any] = [
kCGImageSourceShouldCache: true
]
private static let imageSourceAsyncOptions: [CFString: Any] = [
kCGImageSourceShouldCacheImmediately: true,
kCGImageSourceCreateThumbnailFromImageAlways: true
]
func frameSize(at index: Int, subsamplingLevel: SubsamplingLevel = .default) -> CGSize? {
guard let properties = CGImageSourceCopyPropertiesAtIndex(imageSource, index, imageSourceOptions(with: subsamplingLevel)) as? [CFString: Any] else {
return nil
}
guard let width = properties[kCGImagePropertyPixelWidth] as? Int, let height = properties[kCGImagePropertyPixelHeight] as? Int else {
return nil
}
return CGSize(width: width, height: height)
func frameDuration(at index: Int) -> TimeInterval? {
guard let frameProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, index, imageSourceOptions()) as? [CFString: Any] else {
return nil
}
var animationProperties = ImageDecoder.animationProperties(from: frameProperties)
if animationProperties == nil {
if let properties = CGImageSourceCopyProperties(imageSource, imageSourceOptions()) as? [CFString: Any] {
animationProperties = ImageDecoder.animationHEICSProperties(from: properties, at: index)
var frameCount: Int {
CGImageSourceGetCount(imageSource)
}
private(set) var isAllDataReceived: Bool = false
func setData(_ data: Data, allDataReceived: Bool) {
assert(!isAllDataReceived)
isAllDataReceived = allDataReceived
CGImageSourceUpdateData(imageSource, data as CFData, allDataReceived)
}
class ImageDecoder {
init() {
imageSource = CGImageSourceCreateIncremental(nil)
}
private let imageSource: CGImageSource
}
let modelDescription = CoreDataModelDescription(
entities: [
.entity(
name: "Author",
managedObjectClass: Author.self,
attributes: [
.attribute(name: "name", type: .stringAttributeType)
],
relationships: [
.relationship(name: "publications", destination: "Publication", toMany: true, deleteRule: .cascadeDeleteRule, inverse: "author")
let model = NSManagedObjectModel()
model.entities = [ authorEntity, publicationEntity, articleEntity ]
final class Author: NSManagedObject {
@NSManaged var name: String?
@NSManaged public var publications: NSSet?
}
class Publication: NSManagedObject {
@NSManaged var publicationDate: Date?
@NSManaged var numberOfViews: Int64
@NSManaged var author: Author?
}