【发布时间】:2018-01-13 06:40:59
【问题描述】:
我的屏幕上有多个按钮。
当您单击按钮时,它会调用一个函数,该函数使用 UIImagePickerController 从库或相机中选择图像并将该图像保存到按钮图标中。 (所以基本上是可点击的图片)。
我现在正在尝试添加一些持久性,以便在重新启动应用程序时按钮保存其分配的图像。我还没有找到一个我可以遵循的答案来解释如何实现这一点。
我已将 Core Data 框架添加到我的项目中。任何人都可以建议如何进行。 - 谢谢。
var newPicImg : UIButton?;
@IBOutlet weak var ButtImg1: UIButton!
@IBOutlet weak var ButtImg2: UIButton!
@IBOutlet weak var ButtImg3: UIButton!
按钮:
@IBAction func chooseImage(_ sender: UIButton) {
newPicImg = ButtImg1
picStuff()
}
@IBAction func chooseImage1(_ sender: UIButton) {
newPicImg = ButtImg2
picStuff()
}
@IBAction func chooseImage2(_ sender: UIButton) {
newPicImg = ButtImg3
picStuff()
}
拍摄/选择图片:
func picStuff()
{
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
let actionSheet = UIAlertController(title: "Photo Source", message: "Choose a source", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action:UIAlertAction)in
imagePickerController.sourceType = .camera
self.present(imagePickerController, animated: true, completion: nil)
}))
actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: {(action:UIAlertAction)in
imagePickerController.sourceType = .photoLibrary
self.present(imagePickerController, animated: true, completion: nil)
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil ))
self.present(actionSheet, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
newPicImg?.setImage(image, for: .normal)
picker.dismiss(animated: true, completion: nil)
}
【问题讨论】:
标签: ios iphone swift3 ios10 xcode8