IOS11부터 UIImagePickerController 권한체크가 필요없어졌습니다. 카메라 및 앨범 이미지 가져오는 예제를 만들어 봤습니다.
ImagePickerRepresentable 클래스
import SwiftUI
import Photos
struct ImagePickerRepresentable: UIViewControllerRepresentable {
@Binding var cameraImage: UIImage?
var sourceType: UIImagePickerController.SourceType
@Environment(\.presentationMode) var isPresented
var didFinishCloser: (() -> Void)?
func makeUIViewController(context: Context) -> UIImagePickerController {
let imagePicker = UIImagePickerController()
imagePicker.sourceType = sourceType
imagePicker.allowsEditing = true
imagePicker.delegate = context.coordinator
return imagePicker
}
func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {
}
func makeCoordinator() -> Coordinator {
return Coordinator(picker: self)
}
// Coordinator will help to preview the selected image in the View.
class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var picker: ImagePickerRepresentable
init(picker: ImagePickerRepresentable) {
self.picker = picker
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
//PhotoLibrary 사용시 Asset 사용 가능
//guard let imageAsset = info[.phAsset] as? PHAsset else { return }
guard let image = info[.originalImage] as? UIImage else { return }
self.picker.cameraImage = image
self.picker.isPresented.wrappedValue.dismiss()
self.picker.didFinishCloser?()
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.picker.isPresented.wrappedValue.dismiss()
}
}
}
[사용방법]
import SwiftUI
import Photos
struct ContentView: View {
@StateObject var imagePickerManager = ImagePickerManager()
@State var isShowPickerCameraView: Bool = false
@State var isShowPickerPhotoView: Bool = false
@State var image: UIImage?
var body: some View {
VStack {
// 가져온 이미지 출력 View
Image(uiImage: image ?? UIImage())
.resizable()
.frame(width: 100, height: 100)
.aspectRatio(contentMode: .fit)
//MARK: 카메라 이미지 촬영
Button {
isShowPickerCameraView.toggle()
} label: {
HStack {
Image(systemName: "globe")
Text("카메라 이미지 촬영")
}
}
.fullScreenCover(isPresented: $isShowPickerCameraView,
content: {
// 퍼미션 체크 static 으로 구성 후 외부에서 체크 후 진행 되게 변경 처리
ImagePickerRepresentable(cameraImage: $image, sourceType: .camera) {
//이미지 완료 complete
}
.ignoresSafeArea() //카메라 롤 상/하 SafeArea 무시해야됨. 안하면 흰색으로 표시됨.
})
//MARK: 앨범 이미지 가져오기
Button {
isShowPickerPhotoView.toggle()
} label: {
HStack {
Image(systemName: "globe")
Text("앨범 이미지 가져오기")
}
}
.fullScreenCover(isPresented: $isShowPickerPhotoView, content: {
ImagePickerRepresentable(cameraImage: $image, sourceType: .photoLibrary) {
//이미지 완료 complete
}
.ignoresSafeArea()
})
}
}
}
'SwiftUI' 카테고리의 다른 글
[SwiftUI] PHPickerViewController 멀티선택 사진앨범 가져오기(IOS 14이상) (0) | 2024.05.31 |
---|---|
[SwiftUI] Permission Manager (0) | 2024.05.30 |
[SwiftUI] .fullScreenCover 로 NavigationView 열기 (0) | 2024.05.30 |
[SwiftUI] TabView Show / Hide (TabView - NavigationView) (0) | 2024.05.29 |
[SwiftUI] Custom Present ViewController (0) | 2024.05.29 |