WallPaperHome722/WallpaperHD_Live/Mine/C/WP_PhotoAlbumVC.swift
2024-07-23 11:43:02 +08:00

141 lines
4.5 KiB
Swift

//
// WP_PhotoAlbumVC.swift
// WallpaperHD_Live
//
// Created by 16 on 2024/7/23.
//
import UIKit
import Photos
class WP_PhotoAlbumVC: WP_RootVC {
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var dataView: UIView!
var model = [WP_PhotoAlbum]()
override func viewDidLoad() {
super.viewDidLoad()
setCollectionView()
}
func setCollectionView(){
collectionView.delegate = self
collectionView.dataSource = self
// collection view
let layout = CustomCollectionViewFlowLayout()
collectionView.collectionViewLayout = layout
layout.scrollDirection = .vertical
collectionView.register(UINib(nibName: "WP_WallpaperCollectionCell", bundle: nil), forCellWithReuseIdentifier: "WP_WallpaperCollectionCell")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.isHidden = true
let retrievedModels = PreferencesManager.shared.retrieveRakModels()
if retrievedModels != nil{
self.model = retrievedModels!
}else{
}
if self.model.count == 0{
self.dataView.isHidden = false
}else{
self.dataView.isHidden = true
}
}
@IBAction func add(_ sender: Any) {
let imagePicker = UIImagePickerController()
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self
present(imagePicker, animated: true, completion: nil)
}
@IBAction func back(_ sender: Any) {
self.navigationController?.popViewController(animated: true)
}
// UIImage
func convertImageToString(image: UIImage) -> String? {
if let imageData = image.jpegData(compressionQuality: 1.0) {
// Base64
let base64String = imageData.base64EncodedString()
return base64String
}
return nil
}
// Base64
func convertStringToImage(base64String: String) -> UIImage? {
if let imageData = Data(base64Encoded: base64String) {
if let image = UIImage(data: imageData) {
return image
}
}
return nil
}
}
extension WP_PhotoAlbumVC:UIImagePickerControllerDelegate, UINavigationControllerDelegate{
//
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true, completion: nil)
//
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
// UserDefaults
// saveImageToUserDefaults(image, forKey: "selectedImage")
let base64imgString = convertImageToString(image: image)
let dataModel1 = WP_PhotoAlbum( img: base64imgString)
PreferencesManager.shared.addRakModel(dataModel1)
let retrievedModels = PreferencesManager.shared.retrieveRakModels()
if retrievedModels != nil{
self.model = retrievedModels!
self.collectionView.reloadData()
}else{
}
if self.model.count == 0{
self.dataView.isHidden = false
}else{
self.dataView.isHidden = true
}
}
}
//
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
}
extension WP_PhotoAlbumVC:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return model.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WP_WallpaperCollectionCell", for: indexPath)as!WP_WallpaperCollectionCell
let data = self.model[indexPath.row]
cell.wpImagV.image = convertStringToImage(base64String: data.img ?? "")
return cell
}
}