diff --git a/Memola/Canvas/Contexts/GraphicContext.swift b/Memola/Canvas/Contexts/GraphicContext.swift
index 33c24bc..9862b5c 100644
--- a/Memola/Canvas/Contexts/GraphicContext.swift
+++ b/Memola/Canvas/Contexts/GraphicContext.swift
@@ -139,8 +139,9 @@ extension GraphicContext {
}
}
case 1:
- #warning("TODO: implement photo")
- break
+ guard let photo = element.photo, photo.imageURL != nil else { return }
+ let _photo = Photo(object: photo)
+ tree.insert(_photo.element, in: _photo.photoBox)
default:
break
}
@@ -150,9 +151,8 @@ extension GraphicContext {
}
func loadQuads(_ bounds: CGRect, on context: NSManagedObjectContext) {
- #warning("TODO: implement photo")
- for _stroke in self.tree.search(box: bounds.box) {
- guard let stroke = _stroke.stroke(as: PenStroke.self), stroke.isEmpty else { continue }
+ for element in self.tree.search(box: bounds.box) {
+ guard let stroke = element.stroke(as: PenStroke.self), stroke.isEmpty else { continue }
stroke.loadQuads(with: self)
}
}
@@ -309,7 +309,26 @@ extension GraphicContext {
let origin = point
let bounds = [origin.x - size.width / 2, origin.y - size.height / 2, origin.x + size.width / 2, origin.y + size.height / 2]
let photo = Photo(url: url, size: size, origin: origin, bounds: bounds, createdAt: .now)
- tree.insert(.photo(photo), in: photo.photoBox)
+ tree.insert(photo.element, in: photo.photoBox)
+ withPersistence(\.backgroundContext) { [_photo = photo, graphicContext = object] context in
+ let photo = PhotoObject(\.backgroundContext)
+ photo.imageURL = _photo.url
+ photo.bounds = _photo.bounds
+ photo.width = _photo.size.width
+ photo.originY = _photo.origin.y
+ photo.originX = _photo.origin.x
+ photo.height = _photo.size.height
+ photo.createdAt = _photo.createdAt
+ let element = ElementObject(\.backgroundContext)
+ element.createdAt = _photo.createdAt
+ element.type = 1
+ element.graphicContext = graphicContext
+ photo.element = element
+ element.photo = photo
+ graphicContext?.elements.add(element)
+ _photo.object = photo
+ try context.saveIfNeeded()
+ }
self.previousElement = .photo(photo)
}
}
diff --git a/Memola/Canvas/Elements/Photo/Photo.swift b/Memola/Canvas/Elements/Photo/Photo.swift
index 0a7902f..b20055f 100644
--- a/Memola/Canvas/Elements/Photo/Photo.swift
+++ b/Memola/Canvas/Elements/Photo/Photo.swift
@@ -24,15 +24,6 @@ final class Photo: @unchecked Sendable, Equatable, Comparable {
var vertexCount: Int = 0
var vertexBuffer: MTLBuffer?
- init(image: UIImage?, size: CGSize, origin: CGPoint, bounds: [CGFloat], createdAt: Date) {
- self.size = size
- self.origin = origin
- self.image = image
- self.bounds = bounds
- self.createdAt = createdAt
- generateVertices()
- }
-
init(url: URL?, size: CGSize, origin: CGPoint, bounds: [CGFloat], createdAt: Date) {
self.size = size
self.origin = origin
@@ -44,7 +35,7 @@ final class Photo: @unchecked Sendable, Equatable, Comparable {
convenience init(object: PhotoObject) {
self.init(
- image: UIImage(data: object.image ?? .init()),
+ url: object.imageURL,
size: .init(width: object.width, height: object.height),
origin: .init(x: object.originX, y: object.originY),
bounds: object.bounds,
@@ -117,4 +108,8 @@ extension Photo {
func isVisible(in bounds: CGRect) -> Bool {
bounds.contains(photoBounds) || bounds.intersects(photoBounds)
}
+
+ var element: Element {
+ .photo(self)
+ }
}
diff --git a/Memola/Persistence/Objects/PhotoObject.swift b/Memola/Persistence/Objects/PhotoObject.swift
index 8595ba7..e43de2c 100644
--- a/Memola/Persistence/Objects/PhotoObject.swift
+++ b/Memola/Persistence/Objects/PhotoObject.swift
@@ -16,6 +16,6 @@ class PhotoObject: NSManagedObject {
@NSManaged var height: CGFloat
@NSManaged var bounds: [CGFloat]
@NSManaged var createdAt: Date?
- @NSManaged var image: Data?
+ @NSManaged var imageURL: URL?
@NSManaged var element: ElementObject?
}
diff --git a/Memola/Resources/Models/MemolaModel.xcdatamodeld/MemolaModel.xcdatamodel/contents b/Memola/Resources/Models/MemolaModel.xcdatamodeld/MemolaModel.xcdatamodel/contents
index a310e6c..f12e8a8 100644
--- a/Memola/Resources/Models/MemolaModel.xcdatamodeld/MemolaModel.xcdatamodel/contents
+++ b/Memola/Resources/Models/MemolaModel.xcdatamodeld/MemolaModel.xcdatamodel/contents
@@ -45,7 +45,7 @@
-
+