mirror of
https://github.com/dscyrescotti/Memola.git
synced 2026-05-17 05:07:11 +02:00
feat: save image in rectangle
This commit is contained in:
@@ -319,7 +319,7 @@ extension GraphicContext {
|
|||||||
// MARK: - Photo
|
// MARK: - Photo
|
||||||
extension GraphicContext {
|
extension GraphicContext {
|
||||||
func insertPhoto(at point: CGPoint, photoItem: PhotoItem) -> Photo {
|
func insertPhoto(at point: CGPoint, photoItem: PhotoItem) -> Photo {
|
||||||
let size = photoItem.dimension
|
let size = photoItem.getDimension()
|
||||||
let origin = point
|
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 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)
|
let photo = Photo(url: photoItem.id, size: size, origin: origin, bounds: bounds, createdAt: .now, bookmark: photoItem.bookmark)
|
||||||
|
|||||||
@@ -77,7 +77,6 @@ extension Photo: Drawable {
|
|||||||
renderEncoder.setFragmentTexture(texture, index: 0)
|
renderEncoder.setFragmentTexture(texture, index: 0)
|
||||||
renderEncoder.setVertexBuffer(vertexBuffer, offset: 0, index: 0)
|
renderEncoder.setVertexBuffer(vertexBuffer, offset: 0, index: 0)
|
||||||
renderEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: vertices.count)
|
renderEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: vertices.count)
|
||||||
vertexBuffer = nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -114,33 +114,35 @@ public class Tool: NSObject, ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func selectPhoto(_ image: UIImage, for canvasID: NSManagedObjectID) {
|
func selectPhoto(_ image: UIImage, for canvasID: NSManagedObjectID) {
|
||||||
guard let resizedImage = resizePhoto(of: image) else { return }
|
guard let (resizedImage, dimension) = resizePhoto(of: image) else { return }
|
||||||
let photoItem = bookmarkPhoto(of: resizedImage, with: canvasID)
|
let photoItem = bookmarkPhoto(of: resizedImage, in: dimension, with: canvasID)
|
||||||
withAnimation {
|
withAnimation {
|
||||||
selectedPhotoItem = photoItem
|
selectedPhotoItem = photoItem
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func resizePhoto(of image: UIImage) -> UIImage? {
|
private func resizePhoto(of image: UIImage) -> (UIImage, CGSize)? {
|
||||||
let targetSize = CGSize(width: 768, height: 768)
|
let targetSize = CGSize(width: 512, height: 512)
|
||||||
let size = image.size
|
let size = image.size
|
||||||
let widthRatio = targetSize.width / size.width
|
let widthRatio = targetSize.width / size.width
|
||||||
let heightRatio = targetSize.height / size.height
|
let heightRatio = targetSize.height / size.height
|
||||||
let newSize = CGSize(
|
let dimension = CGSize(
|
||||||
width: size.width * min(widthRatio, heightRatio),
|
width: size.width * min(widthRatio, heightRatio),
|
||||||
height: size.height * 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)
|
image.draw(in: rect)
|
||||||
let newImage = UIGraphicsGetImageFromCurrentImageContext()
|
let newImage = UIGraphicsGetImageFromCurrentImageContext()
|
||||||
UIGraphicsEndImageContext()
|
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 }
|
guard let data = image.jpegData(compressionQuality: 1) else { return nil }
|
||||||
let fileManager = FileManager.default
|
let fileManager = FileManager.default
|
||||||
guard let directory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else {
|
guard let directory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else {
|
||||||
@@ -167,7 +169,7 @@ public class Tool: NSObject, ObservableObject {
|
|||||||
var photoBookmark: PhotoItem?
|
var photoBookmark: PhotoItem?
|
||||||
do {
|
do {
|
||||||
let bookmark = try file.bookmarkData(options: .minimalBookmark)
|
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 {
|
} catch {
|
||||||
NSLog("[Memola] - \(error.localizedDescription)")
|
NSLog("[Memola] - \(error.localizedDescription)")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,13 +11,13 @@ import Foundation
|
|||||||
struct PhotoItem: Identifiable, Equatable {
|
struct PhotoItem: Identifiable, Equatable {
|
||||||
var id: URL
|
var id: URL
|
||||||
let image: UIImage
|
let image: UIImage
|
||||||
|
let dimension: CGSize
|
||||||
let bookmark: Data
|
let bookmark: Data
|
||||||
|
|
||||||
var dimension: CGSize {
|
func getDimension() -> CGSize {
|
||||||
let size = image.size
|
let maxSize = max(dimension.width, dimension.height)
|
||||||
let maxSize = max(size.width, size.height)
|
let width = dimension.width * 100 / maxSize
|
||||||
let width = size.width * 128 / maxSize
|
let height = dimension.height * 100 / maxSize
|
||||||
let height = size.height * 128 / maxSize
|
|
||||||
return CGSize(width: width, height: height)
|
return CGSize(width: width, height: height)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user