Skip to content

Commit 16d6f53

Browse files
committed
[#69] 원본 이미지 디스크 기반 저장으로 메모리 최적화
1 parent 4e6447d commit 16d6f53

6 files changed

Lines changed: 37 additions & 6 deletions

File tree

Codive/Features/Feed/Presentation/Add/ViewModel/RecordDetailViewModel.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ final class RecordDetailViewModel: ObservableObject {
149149
if let image = await downloadImage(from: feedImage.imageUrl) {
150150
var selectedPhoto = SelectedPhoto(
151151
id: UUID().uuidString,
152-
originalImage: image,
153152
croppedImage: image,
154153
order: index,
155154
clothTags: [] // 기존 태그는 나중에 로드됨

Codive/Features/Home/Presentation/ViewModel/HomeViewModel.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,6 @@ extension HomeViewModel {
379379

380380
let selectedPhoto = SelectedPhoto(
381381
id: UUID().uuidString,
382-
originalImage: recordImage,
383382
croppedImage: recordImage,
384383
order: 1
385384
)

Codive/Shared/Services/Photo/Domain/Entities/PhotoAlbum.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,36 @@ struct PhotoAsset: Identifiable {
2828
// MARK: - SelectedPhoto Entity
2929
struct SelectedPhoto: Identifiable, Equatable, Hashable {
3030
let id: String
31-
let originalImage: UIImage
31+
private(set) var originalImagePath: URL?
3232
var croppedImage: UIImage
3333
var order: Int
3434
var clothTags: [ClothTag] = []
3535
var imageUrl: String? // 수정 모드에서 기존 이미지 URL 저장
3636
var aiImageUrl: String? // AI 누끼 이미지 URL
3737

38+
/// 원본 이미지를 tmp 디스크에 저장하고 경로만 보관 (메모리 절약)
39+
mutating func saveOriginalToDisk(_ image: UIImage) {
40+
let path = FileManager.default.temporaryDirectory
41+
.appendingPathComponent("original_\(id).jpg")
42+
if let data = image.jpegData(compressionQuality: 0.9) {
43+
try? data.write(to: path)
44+
originalImagePath = path
45+
}
46+
}
47+
48+
/// 재크롭 시에만 디스크에서 원본 이미지 로드
49+
func loadOriginalImage() -> UIImage? {
50+
guard let path = originalImagePath,
51+
let data = try? Data(contentsOf: path) else { return nil }
52+
return UIImage(data: data)
53+
}
54+
55+
/// tmp 파일 정리
56+
func cleanupOriginal() {
57+
guard let path = originalImagePath else { return }
58+
try? FileManager.default.removeItem(at: path)
59+
}
60+
3861
func hash(into hasher: inout Hasher) {
3962
hasher.combine(id)
4063
hasher.combine(order)

Codive/Shared/Services/Photo/Presentation/PhotoEditView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ struct PhotoEditView: View {
112112
.fullScreenCover(isPresented: $viewModel.isEditingMode) {
113113
if let currentPhoto = viewModel.currentPhoto {
114114
ImageCropView(
115-
image: currentPhoto.originalImage,
115+
image: currentPhoto.loadOriginalImage() ?? currentPhoto.croppedImage,
116116
aspectRatio: viewModel.aspectRatio,
117117
allowZoomOut: viewModel.allowZoomOut,
118118
onComplete: { croppedImage in

Codive/Shared/Services/Photo/Presentation/ViewModel/PhotoEditViewModel.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ final class PhotoEditViewModel: ObservableObject {
105105
}
106106

107107
func completeEditing() {
108+
cleanupOriginalImages()
108109
switch flowType {
109110
case .record:
110111
navigationRouter.navigate(to: .recordDetail(photos: selectedPhotos))
@@ -118,6 +119,15 @@ final class PhotoEditViewModel: ObservableObject {
118119
}
119120

120121
func confirmExit() {
122+
cleanupOriginalImages()
121123
navigationRouter.navigateBack()
122124
}
125+
126+
// MARK: - Private Methods
127+
128+
private func cleanupOriginalImages() {
129+
for photo in selectedPhotos {
130+
photo.cleanupOriginal()
131+
}
132+
}
123133
}

Codive/Shared/Services/Photo/Presentation/ViewModel/RecordAddViewModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,12 @@ final class RecordAddViewModel: ObservableObject {
192192
croppedImage = processImageUseCase.cropTo1_1Ratio(image)
193193
}
194194

195-
let selectedPhoto = SelectedPhoto(
195+
var selectedPhoto = SelectedPhoto(
196196
id: photo.id,
197-
originalImage: image,
198197
croppedImage: croppedImage,
199198
order: index + 1
200199
)
200+
selectedPhoto.saveOriginalToDisk(image)
201201
selectedPhotoItems.append(selectedPhoto)
202202
}
203203
}

0 commit comments

Comments
 (0)