Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

이미지 관련 QA 해결 #147

Merged
merged 8 commits into from
Dec 4, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ final class ModifyBookCoverViewModel: ViewModelType {
return
}
let newBookCover = BookCover(
id: bookID,
order: bookOrder,
title: bookTitle,
imageData: bookImageData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ final class CustomAlbumCollectionViewCell: UICollectionViewCell {

return imageView
}()
var representedAssetIdentifier: String?

// MARK: - Initialize
override init(frame: CGRect) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ final class CustomAlbumViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

bind()
setup()
configureConstraints()
configureNavigationBar()
bind()
checkThumbnailAuthorization()
}

Expand All @@ -81,6 +81,32 @@ final class CustomAlbumViewController: UIViewController {
configureNavigationAppearance()
}

// MARK: - Binding
private func bind() {
let output = viewModel.transform(input: input.eraseToAnyPublisher())

output.receive(on: DispatchQueue.main)
.sink { [weak self] event in
switch event {
case .fetchAssets:
self?.albumCollectionView.reloadData()
case .changedAssets(let changes):
self?.albumCollectionView.performBatchUpdates {
if let inserted = changes.insertedIndexes, !inserted.isEmpty {
self?.albumCollectionView.insertItems(
at: inserted.map({ IndexPath(item: $0 + 1, section: 0) })
)
}
if let removed = changes.removedIndexes, !removed.isEmpty {
self?.albumCollectionView.deleteItems(
at: removed.map({ IndexPath(item: $0 + 1, section: 0) })
)
}
}
}
}.store(in: &cancellables)
}

// MARK: - Setup & Configure
private func setup() {
view.backgroundColor = .baseBackground
Expand All @@ -99,7 +125,6 @@ final class CustomAlbumViewController: UIViewController {
}

private func configureNavigationBar() {
// TODO: - 추후 삭제 필요
navigationController?.navigationBar.isHidden = false
if mediaType == .image {
navigationItem.title = "사진 선택"
Expand Down Expand Up @@ -139,33 +164,6 @@ final class CustomAlbumViewController: UIViewController {
navigationController?.navigationBar.scrollEdgeAppearance = navigationBarAppearance
}

// MARK: - Binding
private func bind() {
let output = viewModel.transform(input: input.eraseToAnyPublisher())

output.receive(on: DispatchQueue.main)
.sink { [weak self] event in
switch event {
case .fetchAssets:
self?.albumCollectionView.reloadData()
case .changedAssets(let changes):
self?.albumCollectionView.performBatchUpdates {
if let inserted = changes.insertedIndexes, !inserted.isEmpty {
self?.albumCollectionView.insertItems(
at: inserted.map({ IndexPath(item: $0 + 1, section: 0) })
)
}
if let removed = changes.removedIndexes, !removed.isEmpty {
self?.albumCollectionView.deleteItems(
at: removed.map({ IndexPath(item: $0 + 1, section: 0) })
)
}
}
}
}
.store(in: &cancellables)
}

// MARK: - Media
private func checkThumbnailAuthorization() {
let authorization = PHPhotoLibrary.authorizationStatus()
Expand Down Expand Up @@ -229,6 +227,7 @@ final class CustomAlbumViewController: UIViewController {
navigationController?.show(imagePicker, sender: nil)
}
}

private func moveToEditPhotoView(image: UIImage?, creationDate: Date) {
guard let photoSelectCompletionHandler else { return }
var editPhotoViewController: EditPhotoViewController
Expand Down Expand Up @@ -281,7 +280,8 @@ extension CustomAlbumViewController: UICollectionViewDelegate {
private func handleImageSelection(with asset: PHAsset) {
Task {
await LocalPhotoManager.shared.requestThumbnailImage(with: asset) { [weak self] image in
guard let self = self, let image = image else { return }
guard let self,
let image else { return }
self.moveToEditPhotoView(image: image, creationDate: asset.creationDate ?? .now)
}
}
Expand Down Expand Up @@ -322,10 +322,13 @@ extension CustomAlbumViewController: UICollectionViewDataSource {
cell.setPhoto(.photo)
} else {
guard let asset = viewModel.photoAsset?[indexPath.item - 1] else { return cell }
cell.representedAssetIdentifier = asset.localIdentifier
let cellSize = cell.bounds.size
Task {
await LocalPhotoManager.shared.requestThumbnailImage(with: asset, cellSize: cellSize) { image in
cell.setPhoto(image)
if cell.representedAssetIdentifier == asset.localIdentifier {
cell.setPhoto(image)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import Photos
actor LocalPhotoManager {
static let shared = LocalPhotoManager()

private let imageManager = PHImageManager()
private let imageManager = PHCachingImageManager()
private let imageRequestOptions: PHImageRequestOptions = {
let options = PHImageRequestOptions()
options.isSynchronous = true
options.isNetworkAccessAllowed = true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문제의 한 줄..

options.deliveryMode = .highQualityFormat

return options
}()
Expand All @@ -29,6 +31,13 @@ actor LocalPhotoManager {
resultHandler: { image, _ in
Task { await completion(image) }
})

imageManager.startCachingImages(
for: [asset],
targetSize: cellSize,
contentMode: .aspectFill,
options: nil
)
}

func requestVideoURL(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,15 @@ final class MHPolaroidPhotoView: UIView {

private func configureConstraints() {
photoImageView.setAnchor(top: topAnchor, constantTop: 25,
width: 280, height: 210)
photoImageView.setCenterX(view: self)
leading: leadingAnchor, constantLeading: 17,
trailing: trailingAnchor, constantTrailing: 17)
NSLayoutConstraint.activate([
photoImageView.heightAnchor.constraint(equalTo: photoImageView.widthAnchor, multiplier: 0.75)
])

captionLabel.setAnchor(top: photoImageView.bottomAnchor, constantTop: 12,
leading: photoImageView.leadingAnchor)
leading: photoImageView.leadingAnchor,
trailing: photoImageView.trailingAnchor)

creationDateLabel.setAnchor(bottom: bottomAnchor, constantBottom: 7,
trailing: trailingAnchor, constantTrailing: 12)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ final class EditPhotoViewController: UIViewController {
configureNavigationBar()
configureAddSubView()
configureConstraints()
configureButtonAction()
configureAction()
}

override func viewWillAppear(_ animated: Bool) {
Expand Down Expand Up @@ -213,7 +213,7 @@ final class EditPhotoViewController: UIViewController {
dividedLine2.setBottom(anchor: editButtonStackView.topAnchor, constant: 11)
captionTextField.setAnchor(
leading: view.leadingAnchor, constantLeading: 13,
trailing: view.trailingAnchor,
trailing: view.trailingAnchor, constantTrailing: 13,
height: 30
)
captionTextFieldBottomConstraint = captionTextField.bottomAnchor.constraint(
Expand Down Expand Up @@ -269,18 +269,26 @@ final class EditPhotoViewController: UIViewController {
)
}

// MARK: - Add Button Action
private func configureButtonAction() {
private func configureAction() {
let rotateButtonAction = UIAction { [weak self] _ in
guard let self else { return }
let image = self.photoImageView.image
self.photoImageView.image = image?.rotate(radians: .pi / 2)
}
rotateButton.addAction(rotateButtonAction, for: .touchUpInside)

let drawButtonAction = UIAction { _ in
// TODO: - Draw Action
}
rotateButton.addAction(rotateButtonAction, for: .touchUpInside)
drawButton.addAction(drawButtonAction, for: .touchUpInside)

let textFieldAction = UIAction { [weak self] _ in
guard let self else { return }
if self.captionTextField.text?.count ?? 0 > 50 {
self.captionTextField.text = String(self.captionTextField.text?.prefix(50) ?? "")
}
}
captionTextField.addAction(textFieldAction, for: .editingChanged)
}

// MARK: - Keyboard Appear & Hide
Expand Down
Loading