【发布时间】:2017-01-08 19:12:05
【问题描述】:
如何在我的应用中使用照片库中的图片?我已经完成了有关创建照片应用程序的教程。这一切都很好。我有一个UIPickerController,可以用相机拍照,保存到库中,或者从库中选择一张图片,然后放到屏幕上。但是……
我想要的是用户选择一张图片,记住图片的名称或编号什么的,然后在另一个页面上使用库中的这张图片。
就像选择头像一样。将图片保存在某处,每当用户进入个人资料页面时,打开之前选择的头像图片。所以我需要的是图片的“名称”(表示),但我找不到。我该怎么做?
(下面的代码只是工作的cam / lib应用程序部分,但它保存了没有名称的图片,这就是问题:如何找出之前保存的图片?)抱歉,变量名是荷兰语。
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var plaatje: UIImageView!
@IBAction func saveknop(_ sender: UIButton) {
let imageData = UIImageJPEGRepresentation(plaatje.image!, 0.6)
let compressedfoto = UIImage(data: imageData!)
UIImageWriteToSavedPhotosAlbum(compressedfoto!, nil, nil, nil)
saveNotice()
}
@IBAction func cameraknop(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
@IBAction func biepknop(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
plaatje.image = image
self.dismiss(animated: true, completion: nil)
}
func saveNotice() {
let alert = UIAlertController(title: "foto genomen", message: "je foto is bewaard in de biep", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(defaultAction)
present(alert, animated: true, completion: nil)
}
}
【问题讨论】:
-
P.S.我意识到(感谢 Jared!)如果图片应该被用户删除,我会得到一个错误,但我希望为此构建一个解决方案(显示一个问号或其他东西)。这样用户就可以制作一张新照片。
标签: ios swift uiimagepickercontroller