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
|
|
|
|
|
|
|
|
extension MastodonRegisterViewController: CropViewControllerDelegate, 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, _ in
|
|
|
|
guard let self = self, let image = image as? UIImage else { return }
|
|
|
|
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)
|
|
|
|
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
|
|
|
|
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-02 09:19:20 +01:00
|
|
|
self.photoButton.setImage(image, for: .normal)
|
|
|
|
cropViewController.dismiss(animated: true, completion: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func avatarButtonPressed(_ sender: UIButton) {
|
|
|
|
var configuration = PHPickerConfiguration()
|
|
|
|
configuration.filter = .images
|
2021-03-02 09:27:58 +01:00
|
|
|
|
2021-03-02 09:19:20 +01:00
|
|
|
let imagePicker = PHPickerViewController(configuration: configuration)
|
|
|
|
imagePicker.delegate = self
|
|
|
|
self.present(imagePicker, animated: true, completion: nil)
|
|
|
|
}
|
|
|
|
}
|