Skip to content

Commit 668cf45

Browse files
committed
✨ [feat] #36 사진 추가 버튼 클릭시 갤러리에서 사진 1개 선택해오도록 구현
1 parent 1bbf267 commit 668cf45

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// Untitled.swift
3+
// FunForYou
4+
//
5+
// Created by 배현진 on 6/4/25.
6+
//
7+
import PhotosUI
8+
import SwiftUI
9+
10+
struct PhotoPicker: UIViewControllerRepresentable {
11+
@Binding var selectedImage: UIImage?
12+
13+
func makeUIViewController(context: Context) -> PHPickerViewController {
14+
var config = PHPickerConfiguration()
15+
config.selectionLimit = 1
16+
config.filter = .images
17+
18+
let picker = PHPickerViewController(configuration: config)
19+
picker.delegate = context.coordinator
20+
return picker
21+
}
22+
23+
func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) {}
24+
25+
func makeCoordinator() -> Coordinator {
26+
Coordinator(self)
27+
}
28+
29+
class Coordinator: NSObject, PHPickerViewControllerDelegate {
30+
let parent: PhotoPicker
31+
32+
init(_ parent: PhotoPicker) {
33+
self.parent = parent
34+
}
35+
36+
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
37+
picker.dismiss(animated: true)
38+
39+
guard let itemProvider = results.first?.itemProvider,
40+
itemProvider.canLoadObject(ofClass: UIImage.self) else { return }
41+
42+
itemProvider.loadObject(ofClass: UIImage.self) { image, _ in
43+
DispatchQueue.main.async {
44+
self.parent.selectedImage = image as? UIImage
45+
}
46+
}
47+
}
48+
}
49+
}

FunForYou/FunForYou/Sources/Presentation/Daily/DailyWriting/DailyWritingView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct DailyWritingView: View {
2929
)
3030
}
3131
.sheet(isPresented: $viewModel.state.isShowImagePicker) {
32-
// TODO: - 이미지 picker 구현해서 띄우기
32+
PhotoPicker(selectedImage: $viewModel.state.selectedImage)
3333
}
3434
.hideKeyboardOnTap()
3535
}

0 commit comments

Comments
 (0)