I am trying to simply allow the user to select an image from their library, and have that replace the default background. It all seems to work up until the image should be replaced, and I get stuck with the same default image.
我只是想让用户从他们的库中选择一个图像,并让它替换默认背景。这一切似乎都是正常的,直到图像应该被替换,然后我就陷入了相同的默认图像。
For clarity: The image picker is being presented, allowing the user to select an image, it then gets dismissed, but does not replace the background image.
为了清晰:图像选择器正在被呈现,允许用户选择一个图像,然后它被删除,但是不替换背景图像。
import UIKit
class CharmanderViewController: UIViewController {
@IBOutlet weak var background: UIImageView!
@IBOutlet weak var charmander: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
background.addSubview(charmander)
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(pan(_ :)))
charmander.addGestureRecognizer(panGesture)
let scaleGesture = UIPinchGestureRecognizer(target: self, action: #selector(scale(_ :)))
charmander.addGestureRecognizer(scaleGesture)
}
@objc func scale (_ sender:UIPinchGestureRecognizer){
if let view = sender.view {
view.transform = view.transform.scaledBy(x: sender.scale, y: sender.scale)
}
sender.scale = 1.0
}
@objc func pan (_ sender:UIPanGestureRecognizer){
let translation = sender.translation(in: self.view)
if let view = sender.view {
view.center = CGPoint(x: view.center.x + translation.x,
y: view.center.y + translation.y)
}
sender.setTranslation(CGPoint.zero, in: self.view)
if sender.state == .began {
print ("drag started")
} else if sender.state == .changed {
print ("dragging")
} else if sender.state == .ended {
print ("drag ended")
}
}
@IBAction func getImage(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self as? UIImagePickerControllerDelegate & UINavigationControllerDelegate
imagePicker.sourceType = .photoLibrary
imagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
background.image = info[UIImagePickerControllerOriginalImage] as? UIImage
dismiss(animated: true, completion: nil)
}
@IBAction func Share(_ sender: Any) {
let renderer = UIGraphicsImageRenderer (size: background.bounds.size)
let image = renderer.image { context in
background.drawHierarchy(in: background.bounds, afterScreenUpdates: true)
}
let shareViewController = UIActivityViewController(activityItems: [image], applicationActivities: [])
present (shareViewController, animated: true)
}
}
I've tried using
我试着使用
DispatchQueue.main.async {
updateYourUI()
}
but I can't quite figure out how to correctly write a function that replaces the image. Any help is appreciated.
但是我不知道如何正确地写出一个替换图像的函数。任何帮助都是感激。
1 个解决方案
#1
1
Try adding your UIImagePickerControllerDelegate and UINavigationControllerDelegate at the top of the file:
尝试在文件的顶部添加您的UIImagePickerControllerDelegate和UINavigationControllerDelegate:
class CharmanderViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
and in your getImage() func, set the imagePickerController delegate to just self:
在getImage() func中,将imagePickerController委托设置为self:
@IBAction func getImage(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.mediaTypes =
UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
self.present(imagePicker, animated: true, completion: nil)
}
}
And in your didFinishPickingMediaWithInfo method, make a check to see if you can cast the info as UIImage:
在didFinishPickingMediaWithInfo方法中,检查一下是否可以将信息转换为UIImage:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage else {return}
background.image = originalImage
dismiss(animated: true, completion: nil)
}
#1
1
Try adding your UIImagePickerControllerDelegate and UINavigationControllerDelegate at the top of the file:
尝试在文件的顶部添加您的UIImagePickerControllerDelegate和UINavigationControllerDelegate:
class CharmanderViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
and in your getImage() func, set the imagePickerController delegate to just self:
在getImage() func中,将imagePickerController委托设置为self:
@IBAction func getImage(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.mediaTypes =
UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
self.present(imagePicker, animated: true, completion: nil)
}
}
And in your didFinishPickingMediaWithInfo method, make a check to see if you can cast the info as UIImage:
在didFinishPickingMediaWithInfo方法中,检查一下是否可以将信息转换为UIImage:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage else {return}
background.image = originalImage
dismiss(animated: true, completion: nil)
}