Kurdtvs-Live-Kurdish-TV-Kur.../Mastodon/Scene/Onboarding/Register/MastodonRegisterViewControl...

64 lines
2.6 KiB
Swift
Raw Normal View History

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
// 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
}
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
cropController.setAspectRatioPreset(.presetSquare, animated: true)
cropController.aspectRatioPickerButtonHidden = true
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-02 09:27:58 +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) {
self.viewModel.avatarImage.value = image
self.avatarButton.setImage(image, for: .normal)
2021-03-02 09:19:20 +01:00
cropViewController.dismiss(animated: true, completion: nil)
}
}
2021-03-02 09:19:20 +01:00
extension MastodonRegisterViewController {
2021-03-02 09:19:20 +01:00
@objc func avatarButtonPressed(_ sender: UIButton) {
self.present(imagePicker, animated: true, completion: nil)
}
}