ios - Swift 3 - 如何在应用程序中使用照片库图片?

标签 ios swift uiimagepickercontroller

如何在我的应用程序中使用照片库中的图片?我已经完成了有关创建照片应用程序的教程。一切都很好。我有一个 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)

    }
}

最佳答案

我发现的一个解决方案是使用 UIImageJPEGRepresentationUIImagePNGRepresentation 将图像转换为数据对象。从那里,您可以使用 write(to:) 方法将其保存到您希望应用使用的特定 URL。

this的答案SO问题展示了存储图像的几种方法。可能会有帮助。

关于ios - Swift 3 - 如何在应用程序中使用照片库图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41536697/

相关文章:

iphone - iOS + MacOS 应用程序付费应用程序,一次购买并在所有设备上享受的最佳用户体验?

ios - 尝试在 webKit 上获取返回按钮

swift - 按子查询 Firebase 数据

ios - LinkingObject 的用法

ios - 有没有办法获取我的 iOS 应用程序屏幕截图列表?

javascript - 混合移动应用程序

ios - 如何使用 NSURLProtocol 在 iOS 中拦截网络调用 (REST)

按名称快速查找场景

uiimagepickercontroller - 尝试在路径文件 :///var/mobile/Media/PhotoData/Photos. sqlite 中添加只读文件?

ios - 返回的 UIImagePickerController 图像大于 iPhone 4 上的预览