如何在我的应用程序中使用照片库中的图片?我已经完成了有关创建照片应用程序的教程。一切都很好。我有一个 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)
}
}
最佳答案
我发现的一个解决方案是使用 UIImageJPEGRepresentation 或 UIImagePNGRepresentation 将图像转换为数据对象。从那里,您可以使用 write(to:) 方法将其保存到您希望应用使用的特定 URL。
this的答案SO问题展示了存储图像的几种方法。可能会有帮助。
关于ios - Swift 3 - 如何在应用程序中使用照片库图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41536697/