feat: save image in rectangle

This commit is contained in:
dscyrescotti
2024-06-23 17:24:25 +07:00
parent 96bf8056a9
commit 061be60b35
4 changed files with 18 additions and 17 deletions

View File

@@ -319,7 +319,7 @@ extension GraphicContext {
// MARK: - Photo
extension GraphicContext {
func insertPhoto(at point: CGPoint, photoItem: PhotoItem) -> Photo {
let size = photoItem.dimension
let size = photoItem.getDimension()
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: photoItem.id, size: size, origin: origin, bounds: bounds, createdAt: .now, bookmark: photoItem.bookmark)

View File

@@ -77,7 +77,6 @@ extension Photo: Drawable {
renderEncoder.setFragmentTexture(texture, index: 0)
renderEncoder.setVertexBuffer(vertexBuffer, offset: 0, index: 0)
renderEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: vertices.count)
vertexBuffer = nil
}
}

View File

@@ -114,33 +114,35 @@ public class Tool: NSObject, ObservableObject {
}
func selectPhoto(_ image: UIImage, for canvasID: NSManagedObjectID) {
guard let resizedImage = resizePhoto(of: image) else { return }
let photoItem = bookmarkPhoto(of: resizedImage, with: canvasID)
guard let (resizedImage, dimension) = resizePhoto(of: image) else { return }
let photoItem = bookmarkPhoto(of: resizedImage, in: dimension, with: canvasID)
withAnimation {
selectedPhotoItem = photoItem
}
}
private func resizePhoto(of image: UIImage) -> UIImage? {
let targetSize = CGSize(width: 768, height: 768)
private func resizePhoto(of image: UIImage) -> (UIImage, CGSize)? {
let targetSize = CGSize(width: 512, height: 512)
let size = image.size
let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height
let newSize = CGSize(
let dimension = CGSize(
width: size.width * min(widthRatio, heightRatio),
height: size.height * min(widthRatio, heightRatio)
)
let rect = CGRect(origin: .zero, size: newSize)
let rect = CGRect(origin: .zero, size: targetSize)
UIGraphicsBeginImageContextWithOptions(newSize, true, 1.0)
UIGraphicsBeginImageContextWithOptions(targetSize, true, 1.0)
image.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
guard let newImage else { return nil }
return (newImage, dimension)
}
private func bookmarkPhoto(of image: UIImage, with canvasID: NSManagedObjectID) -> PhotoItem? {
private func bookmarkPhoto(of image: UIImage, in dimension: CGSize, with canvasID: NSManagedObjectID) -> PhotoItem? {
guard let data = image.jpegData(compressionQuality: 1) else { return nil }
let fileManager = FileManager.default
guard let directory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else {
@@ -167,7 +169,7 @@ public class Tool: NSObject, ObservableObject {
var photoBookmark: PhotoItem?
do {
let bookmark = try file.bookmarkData(options: .minimalBookmark)
photoBookmark = PhotoItem(id: file, image: image, bookmark: bookmark)
photoBookmark = PhotoItem(id: file, image: image, dimension: dimension, bookmark: bookmark)
} catch {
NSLog("[Memola] - \(error.localizedDescription)")
}

View File

@@ -11,13 +11,13 @@ import Foundation
struct PhotoItem: Identifiable, Equatable {
var id: URL
let image: UIImage
let dimension: CGSize
let bookmark: Data
var dimension: CGSize {
let size = image.size
let maxSize = max(size.width, size.height)
let width = size.width * 128 / maxSize
let height = size.height * 128 / maxSize
func getDimension() -> CGSize {
let maxSize = max(dimension.width, dimension.height)
let width = dimension.width * 100 / maxSize
let height = dimension.height * 100 / maxSize
return CGSize(width: width, height: height)
}
}