使用图像选择器更新swift中的背景图像

时间:2018-03-08 01:36:00

标签: ios swift uiimagepickercontroller

我试图简单地允许用户从他们的库中选择一个图像,然后替换默认背景。这一切似乎都要工作,直到图像应该被替换,我陷入了相同的默认图像。

为清晰起见:正在呈现图像选择器,允许用户选择图像,然后被解除,但不会替换背景图像。

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)
}
}

我尝试过使用

DispatchQueue.main.async {
    updateYourUI()
}

但我无法弄清楚如何正确编写替换图像的函数。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

尝试在文件顶部添加UIImagePickerControllerDelegate和UINavigationControllerDelegate:

class CharmanderViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

并在你的getImage()函数中,将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)
    }
}

在你的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)
}