我正在尝试制作一个包含 15 个应用程序的简单 iOS 游戏,我可以在其中获取用户的输入图像并用它制作游戏。到目前为止我已经弄清楚了其他所有内容,但我不知道如何访问用户的输入图像。
因此,我按照指南的指示进行操作,并设法在第一页中获取图像,如屏幕截图中所示。 但是,我不知道如何保存图像,将其分成十六个图 block 并重新发布到第二页,以便用户可以玩图 block 。
所以我的问题是 1)如何将用户输入的图像从第一页移动到第二页?或者如何保存图像?
2) 如何将图像分割为 16 个图 block 并保存它们或相应地将它们放入每个 ImageView 中?
我在第一页的代码是
import UIKit
class BeginningPage: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var ImageView: UIImageView!
@IBOutlet weak var ImageSelector: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
// Dismiss the picker if the user canceled.
dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
ImageView.image = selectedImage
// Dismiss the picker.
dismiss(animated: true, completion: nil)
}
//MARK: Action
@IBAction func PressImage(_ sender: UITapGestureRecognizer) {
let imagePickerController = UIImagePickerController()
imagePickerController.sourceType = .photoLibrary
imagePickerController.delegate = self
present(imagePickerController, animated: true, completion: nil)
}
}
最佳答案
您可以通过将图像设置为第二个 View Controller 上的属性,将图像从第一个“页面”传递到第二个“页面”。 (或者更好的方法可能是使用共享 View 模型对象。
研究在第一个 View Controller 中实现 prepareForSegue:
。在那里,您可以将图像传递给第二个 View Controller 。一般步骤是:
- 在 Storyboard中,向转场添加标识符。
- 在第二个 View Controller 中,添加图像属性。 (也许在
didSet
内部,您可以根据需要分割图像并将其添加到第二个 View Controller 。) - 在第一个 View Controller 中,实现
prepareForSegue:
,如果 segue 的标识符与您在第 1 步中设置的标识符匹配,则可以在第 2 步中创建的属性上设置图像.(您可能需要对 segue 的目标 View Controller 进行类型转换。)
你的第二个问题有点复杂。您可以首先查看 CGImageCreateWithImageInRect
的文档这将让您将较大的图像分解为较小的图像。
关于swift - 通过 Swift 在 Xcode 中保存和使用用户输入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43960341/