Skip to content

Commit

Permalink
YAPP-19th#61 - 유저 이미지 수정 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
ITlearning committed Jan 12, 2022
1 parent de303a3 commit 6088e53
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Budi/Budi/Resource/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>앨범 사용 권한을 허용합니다.</string>
<key>NSCameraUsageDescription</key>
<string>카메라 사용 권한을 허용합니다.</string>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
Expand Down
86 changes: 84 additions & 2 deletions Budi/Budi/Source/MyBudi/Edit/MyBudiEditViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
import UIKit
import Combine
import CombineCocoa
import Kingfisher
import PhotosUI

final class MyBudiEditViewController: UIViewController {

@IBOutlet weak var imageEditButton: UIButton!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var userImageView: UIImageView!
weak var coordinator: MyBudiCoordinator?
var viewModel: MyBudiEditViewModel
private var cancellables = Set<AnyCancellable>()
var location: String = "충남 당진시"
var editIndex: Int = -1

init(nibName: String?, bundle: Bundle?, viewModel: MyBudiEditViewModel) {
Expand All @@ -32,6 +35,7 @@ final class MyBudiEditViewController: UIViewController {
configureNavigationBar()
configureTableView()
setPublisher()
configureLayout()
}

override func viewWillAppear(_ animated: Bool) {
Expand All @@ -58,6 +62,14 @@ final class MyBudiEditViewController: UIViewController {
}
.store(in: &cancellables)

imageEditButton.tapPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
guard let self = self else { return }
self.editUserImageAlert()
}
.store(in: &cancellables)

}

func modalViewBackgoundOn() {
Expand Down Expand Up @@ -124,7 +136,7 @@ final class MyBudiEditViewController: UIViewController {
}
}

// MARK: - CollectionView
// MARK: - ConfigureTableView
extension MyBudiEditViewController {
private func configureTableView() {
tableView.delegate = self
Expand All @@ -145,6 +157,7 @@ extension MyBudiEditViewController {
}
}

// MARK: - ConfigureViewController
private extension MyBudiEditViewController {
func configureNavigationBar() {
let saveButton = UIButton(type: .system)
Expand All @@ -155,8 +168,47 @@ private extension MyBudiEditViewController {
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: stackview)
title = "프로필 수정"
}

func configureLayout() {
guard let url = URL(string: viewModel.state.loginUserData.value?.imgUrl ?? "") else { return }
userImageView.kf.setImage(with: url)
userImageView.contentMode = .scaleAspectFill
}

func editUserImageAlert() {
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let camera = UIAlertAction(title: "카메라", style: .default, handler: { [weak self] _ in
guard let self = self else { return }
let camera = UIImagePickerController()
camera.sourceType = .camera
camera.allowsEditing = true
camera.cameraDevice = .rear
camera.cameraCaptureMode = .photo
camera.delegate = self
self.present(camera, animated: true, completion: nil)
})

let album = UIAlertAction(title: "앨범", style: .default, handler: { [weak self] _ in
guard let self = self else { return }
let album = UIImagePickerController()
album.sourceType = .photoLibrary
album.delegate = self
album.allowsEditing = true

self.present(album, animated: true, completion: nil)
})

let cancel = UIAlertAction(title: "취소", style: .cancel, handler: {_ in })

actionSheet.addAction(camera)
actionSheet.addAction(album)
actionSheet.addAction(cancel)

present(actionSheet, animated: true, completion: nil)
}
}

// MARK: - TableViewDelegate
extension MyBudiEditViewController: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
Expand Down Expand Up @@ -383,3 +435,33 @@ extension MyBudiEditViewController: PortfolioViewControllerDelegate {
tableView.reloadData()
}
}

// MARK: - UIImagePicker
extension MyBudiEditViewController: UIImagePickerControllerDelegate & UINavigationControllerDelegate {

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[UIImagePickerController.InfoKey(rawValue: "UIImagePickerControllerEditedImage")] as? UIImage, let jpegData = image.jpegData(compressionQuality: 0.2) else {
picker.dismiss(animated: true, completion: nil)
return
}
userImageView.image = image

viewModel.convertImageToURL(jpegData, { result in
switch result {
case .success(let response):
do {
guard let json = try JSONSerialization.jsonObject(with: response.data, options: []) as? [String: Any],
let data = json["data"] as? [String: Any],
let imageUrl = data["imageUrl"] as? String else { return }
var changeData = self.viewModel.state.loginUserData.value
changeData?.imgUrl = imageUrl
print("이미지 URL", imageUrl)
picker.dismiss(animated: true)
} catch {}
case .failure(let error):
print(error.localizedDescription)
}

})
}
}
7 changes: 7 additions & 0 deletions Budi/Budi/Source/MyBudi/Edit/MyBudiEditViewController.xib
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="MyBudiEditViewController" customModule="Budi" customModuleProvider="target">
<connections>
<outlet property="imageEditButton" destination="KXH-7h-0vc" id="6gq-2w-KAA"/>
<outlet property="tableView" destination="GkF-Il-M7b" id="lox-VS-Tbf"/>
<outlet property="userImageView" destination="eTX-aD-ITv" id="RfL-Ef-zrj"/>
<outlet property="view" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/>
</connections>
</placeholder>
Expand All @@ -38,6 +40,11 @@
<constraint firstAttribute="width" constant="84" id="ks9-mY-VpJ"/>
<constraint firstAttribute="height" constant="84" id="uap-xW-o17"/>
</constraints>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="cornerRadius">
<real key="value" value="42"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</imageView>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="KXH-7h-0vc">
<rect key="frame" x="139" y="116" width="96" height="30"/>
Expand Down
9 changes: 9 additions & 0 deletions Budi/Budi/Source/MyBudi/Edit/MyBudiEditViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,13 @@ class MyBudiEditViewModel: ViewModel {

action.fetch.send(())
}

func convertImageToURL(_ jpegData: Data, _ completion: @escaping (Result<Moya.Response, Error>) -> Void) {
provider.request(.convertImageToURL(jpegData: jpegData)) { result in
switch result {
case .success(let response): completion(.success(response))
case .failure(let error): completion(.failure(error))
}
}
}
}

0 comments on commit 6088e53

Please sign in to comment.