This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum SubsamplingLevel: Int { | |
case level0 = 1 | |
case level1 = 2 | |
case level2 = 4 | |
case level3 = 8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static let imageSourceOptions: [CFString: Any] = [ | |
kCGImageSourceShouldCache: true | |
] | |
private static let imageSourceAsyncOptions: [CFString: Any] = [ | |
kCGImageSourceShouldCacheImmediately: true, | |
kCGImageSourceCreateThumbnailFromImageAlways: true | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var frameCount: Int { | |
CGImageSourceGetCount(imageSource) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private(set) var isAllDataReceived: Bool = false | |
func setData(_ data: Data, allDataReceived: Bool) { | |
assert(!isAllDataReceived) | |
isAllDataReceived = allDataReceived | |
CGImageSourceUpdateData(imageSource, data as CFData, allDataReceived) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ImageDecoder { | |
init() { | |
imageSource = CGImageSourceCreateIncremental(nil) | |
} | |
private let imageSource: CGImageSource | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let model = NSManagedObjectModel() | |
model.entities = [ authorEntity, publicationEntity, articleEntity ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? | |
} |