2021-03-02 09:19:20 +01:00
|
|
|
//
|
|
|
|
// MastodonRegisterViewController+Avatar.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/3/2.
|
|
|
|
//
|
|
|
|
|
|
|
|
import CropViewController
|
2021-03-02 09:27:58 +01:00
|
|
|
import Foundation
|
2021-03-02 09:19:20 +01:00
|
|
|
import PhotosUI
|
2021-03-02 09:27:58 +01:00
|
|
|
import UIKit
|
|
|
|
|
2021-03-04 08:29:46 +01:00
|
|
|
// MARK: - PHPickerViewControllerDelegate
|
|
|
|
extension MastodonRegisterViewController: PHPickerViewControllerDelegate {
|
2021-03-02 09:19:20 +01:00
|
|
|
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
|
2021-03-02 09:27:58 +01:00
|
|
|
guard let itemProvider = results.first?.itemProvider, itemProvider.canLoadObject(ofClass: UIImage.self) else {
|
|
|
|
picker.dismiss(animated: true, completion: {})
|
|
|
|
return
|
|
|
|
}
|
2021-03-02 10:24:04 +01:00
|
|
|
itemProvider.loadObject(ofClass: UIImage.self) { [weak self] image, error in
|
|
|
|
guard let self = self else { return }
|
|
|
|
guard let image = image as? UIImage else {
|
|
|
|
guard let error = error else { return }
|
|
|
|
let alertController = UIAlertController(for: error, title: "", preferredStyle: .alert)
|
|
|
|
let okAction = UIAlertAction(title: L10n.Common.Controls.Actions.ok, style: .default, handler: nil)
|
|
|
|
alertController.addAction(okAction)
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.coordinator.present(
|
|
|
|
scene: .alertController(alertController: alertController),
|
|
|
|
from: nil,
|
|
|
|
transition: .alertController(animated: true, completion: nil)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-03-02 09:27:58 +01:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
let cropController = CropViewController(croppingStyle: .default, image: image)
|
|
|
|
cropController.delegate = self
|
2021-03-02 09:39:43 +01:00
|
|
|
cropController.setAspectRatioPreset(.presetSquare, animated: true)
|
2021-03-02 10:24:04 +01:00
|
|
|
cropController.aspectRatioPickerButtonHidden = true
|
2021-03-02 09:39:43 +01:00
|
|
|
cropController.aspectRatioLockEnabled = true
|
2021-03-02 09:27:58 +01:00
|
|
|
picker.dismiss(animated: true, completion: {
|
|
|
|
self.present(cropController, animated: true, completion: nil)
|
|
|
|
})
|
2021-03-02 09:19:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-04 08:29:46 +01:00
|
|
|
}
|
2021-03-02 09:27:58 +01:00
|
|
|
|
2021-03-04 08:29:46 +01:00
|
|
|
// MARK: - CropViewControllerDelegate
|
|
|
|
extension MastodonRegisterViewController: CropViewControllerDelegate {
|
2021-03-02 09:19:20 +01:00
|
|
|
public func cropViewController(_ cropViewController: CropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int) {
|
2021-03-02 09:39:43 +01:00
|
|
|
self.viewModel.avatarImage.value = image
|
2021-03-04 08:29:46 +01:00
|
|
|
self.avatarButton.setImage(image, for: .normal)
|
2021-03-02 09:19:20 +01:00
|
|
|
cropViewController.dismiss(animated: true, completion: nil)
|
|
|
|
}
|
2021-03-04 08:29:46 +01:00
|
|
|
}
|
2021-03-02 09:19:20 +01:00
|
|
|
|
2021-03-04 08:29:46 +01:00
|
|
|
extension MastodonRegisterViewController {
|
2021-03-02 09:19:20 +01:00
|
|
|
@objc func avatarButtonPressed(_ sender: UIButton) {
|
|
|
|
self.present(imagePicker, animated: true, completion: nil)
|
|
|
|
}
|
|
|
|
}
|