WallPaperHome722/WallpaperHD_Live/Home/C/WP_WallPaPerDetailsVC.swift
2024-07-25 19:20:41 +08:00

241 lines
8.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// WP_WallPaPerDetailsVC.swift
// WallpaperHD_Live
//
// Created by 16 on 2024/7/22.
//
import UIKit
import SDWebImage
import Photos
import SVProgressHUD
import Alamofire
class WP_WallPaPerDetailsVC: WP_RootVC {
@IBOutlet weak var bgView: UIView!
@IBOutlet weak var backBtn: UIButton!
@IBOutlet weak var preimageV: UIImageView!
let activityView = UIActivityIndicatorView()
var imageName:String?
override func viewDidLoad() {
super.viewDidLoad()
self.preimageV.image = UIImage(named: imageName ?? "1")
// UIView
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(viewTapped))
preimageV.isUserInteractionEnabled = true
preimageV.addGestureRecognizer(tapGesture)
activityView.center = CGPoint(x: self.view.center.x, y: self.view.center.y - kSafeArea_Top - 44)
//
activityView.hidesWhenStopped = true
activityView.color = .black;
activityView.style = UIActivityIndicatorView.Style.whiteLarge
self.view.addSubview(activityView)
backBtn.layer.cornerRadius = 20
}
@IBAction func saveBtn(_ sender: Any) {
self.activityView.startAnimating()
saveImageToPhotoLibrary()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.isHidden = true
}
func saveImageToPhotoLibrary() {
guard let image = self.preimageV.image else {
print("找不到要保存的图片")
SVProgressHUD.showInfo(withStatus: "Can't find the picture to save")
DispatchQueue.global().asyncAfter(deadline: .now() + 2.0) {
SVProgressHUD.dismiss()
}
return
}
PHPhotoLibrary.requestAuthorization { status in
switch status {
case .authorized:
self.saveImage(image: image)
case .denied, .restricted:
print("用户拒绝了访问相册权限")
SVProgressHUD.showInfo(withStatus: "Album access denied")
DispatchQueue.global().asyncAfter(deadline: .now() + 2.0) {
SVProgressHUD.dismiss()
}
case .notDetermined:
print("用户尚未选择是否允许访问相册")
SVProgressHUD.showInfo(withStatus: "You have not chosen whether to allow access to the album")
DispatchQueue.global().asyncAfter(deadline: .now() + 2.0) {
SVProgressHUD.dismiss()
}
@unknown default:
break
}
}
}
func saveImage(image: UIImage) {
PHPhotoLibrary.shared().performChanges({
//
PHAssetChangeRequest.creationRequestForAsset(from: image)
}) { success, error in
if success {
print("图片保存成功")
DispatchQueue.main.asyncAfter(wallDeadline: .now()) {
self.activityView.stopAnimating()
SVProgressHUD.showSuccess(withStatus: "Image saved successfully")
DispatchQueue.global().asyncAfter(deadline: .now() + 1.5) {
SVProgressHUD.dismiss()
}
}
} else if let error = error {
print("图片保存失败:\(error.localizedDescription)")
DispatchQueue.main.asyncAfter(wallDeadline: .now()) {
self.activityView.stopAnimating()
SVProgressHUD.showError(withStatus: "Image saving failed:\(error.localizedDescription)")
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
SVProgressHUD.dismiss()
self.activityView.stopAnimating()
}
}
}
}
}
@IBAction func previewBtn(_ sender: Any) {
UIView.animate(withDuration: 0.5, animations: {
self.bgView.frame.origin.y += 0
self.bgView.alpha = 0
self.backBtn.isHidden = true
})
showView()
}
//
@objc func viewTapped() {
// UIView
if bgView.frame.origin.y == UIScreen.main.bounds.height - bgView.frame.height {
// UIView
UIView.animate(withDuration: 0.5, animations: {
self.bgView.frame.origin.y += self.bgView.frame.height
self.bgView.alpha = 0
self.backBtn.isHidden = true
})
} else {
self.backBtn.isHidden = false
// UIView
UIView.animate(withDuration: 0.5, animations: {
self.bgView.frame.origin.y -= self.bgView.frame.height
self.bgView.alpha = 1
})
}
}
func showView() {
//
let previewView = Bundle.main.loadNibNamed("WP_Preview", owner: nil, options: nil)?.first as! WP_Preview
previewView.touchBlock = {
self.backBtn.isHidden = false
// UIView
UIView.animate(withDuration: 0.5, animations: {
self.bgView.frame.origin.y -= 0
self.bgView.alpha = 1
})
}
view.addSubview(previewView)
//
previewView.frame.origin.x = -previewView.frame.width
//
UIView.animate(withDuration: 1.0) {
//
previewView.frame = self.view.frame
}
}
@IBAction func shareBtn(_ sender: Any) {
self.activityView.startAnimating()
//
guard let image = self.preimageV.image else {
SVProgressHUD.showInfo(withStatus: "Can't find the image to share")
DispatchQueue.global().asyncAfter(deadline: .now() + 2.0) {
SVProgressHUD.dismiss()
}
print("找不到要分享的图片")
return
}
//
let items: [Any] = [image]
// UIActivityViewController
let activityViewController = UIActivityViewController(activityItems: items, applicationActivities: nil)
// UIActivityViewController
activityViewController.modalPresentationStyle = .popover
// iPad UIActivityViewController
if let popoverController = activityViewController.popoverPresentationController {
popoverController.sourceView = self.view
popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
popoverController.permittedArrowDirections = []
}
//
self.present(activityViewController, animated: true, completion: nil)
DispatchQueue.main.asyncAfter(deadline: .now() + 0){
self.activityView.stopAnimating()
}
}
@IBAction func collectBtn(_ sender: Any) {
let manager = ImageNameManager()
//
manager.addImageName(self.imageName ?? "1")
SVProgressHUD.showInfo(withStatus: "Collection successful")
DispatchQueue.global().asyncAfter(deadline: .now() + 2.0) {
SVProgressHUD.dismiss()
}
}
@IBAction func back(_ sender: Any) {
self.navigationController?.popViewController(animated: true)
}
}