答案 0 :(得分:0)
这里我使用了storyBoard中的imagePicker和Indicator 而不是使用Gesture按钮Action 问题是显示活动指标直到图像选择器加载时间
使用以下代码参考
import UIKit
class ProfileViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var profileImageView: UIImageView!
@IBOutlet weak var Spinner: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
Spinner.isHidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func imageSelectAction(_ sender: Any) {
//your tap gesture action like
//show and start animating spinner
//animate till output is not achieved
Spinner.isHidden = false
self.Spinner.startAnimating()
//Initiate a picker here
let picker = UIImagePickerController()
//Delegates
picker.delegate = self
picker.allowsEditing = true
//present to call imagePickerController delegate function
present(picker, animated: true, completion: nil)
}
//delegate method
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
//selected image from picker
var selectedImageFromPicker: UIImage?
//additional checks if required
if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage
{
selectedImageFromPicker = editedImage
}
else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage
{
selectedImageFromPicker = originalImage
}
//here set value in your outlet profile image
if let selectedImage = selectedImageFromPicker
{
profileImageView.image = selectedImage
}
//Hide spinner and stop animating
Spinner.isHidden = true
self.Spinner.stopAnimating()
dismiss(animated: true, completion: nil)
}
//if picker is cancelled in any case without selection
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
print("canceled picker")
//if picker is cancelled so need to hide animating spinner
Spinner.isHidden = true
self.Spinner.stopAnimating()
dismiss(animated: true, completion: nil)
}
}
还可以轻松使用TapGesture我使用了一个按钮,只需在您的点按手势中添加我的按钮动作代码并以轻击手势启动微调器 并在选择器代理中隐藏动画微调器