Swift 3 保存从相机拍摄的图像并在数据库中保存路径

Swift 3 save an image taken from camera and save path on database

我可以用 phone 拍照保存在图库中,但我不知道如何存储图片的路径,所以在我可以从图库中检索它之后(我还不知道如何通过他的路径检索图片)以在我的控制器上显示它。

我有这段代码可以正常工作。我可以看到我的图片保存在图库中,但我不知道如何获取他的 url、 我想保存在 coredata

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    imagePicker.dismiss(animated: true, completion: nil)
    exerciceImage.image = info[UIImagePickerControllerOriginalImage] as? UIImage
    exerciceImage.isHidden = false;

    UIImageWriteToSavedPhotosAlbum(exerciceImage.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
    if let error = error {
        // we got back an error!
        let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "OK", style: .default))
        present(ac, animated: true)
    } else {
        let name = image.accessibilityIdentifier;

        let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "OK", style: .default))
        present(ac, animated: true)
    }
}

我读到我可以使用 PHPhotoLibrary,但它给我的错误是它无法转换为 nsurl 这是我试过的

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    imagePicker.dismiss(animated: true, completion: nil)
    exerciceImage.image = info[UIImagePickerControllerOriginalImage] as? UIImage
    exerciceImage.isHidden = false;

    PHPhotoLibrary.shared().performChanges({
        let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(atFileURL: info[UIImagePickerControllerOriginalImage] as! URL)
        let assetPlaceholder = assetRequest?.placeholderForCreatedAsset
    }, completionHandler: { success, error in
        // completion handling
        let url = info[UIImagePickerControllerOriginalImage] as! URL;
    })
} 

我找到了解决方案here 但我编辑它是因为在应用程序关闭后我无法加载图像,这是我的解决方案希望它能帮助别人 :)

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    imagePicker.dismiss(animated: true, completion: nil)
    exerciceImage.image = info[UIImagePickerControllerOriginalImage] as? UIImage
    exerciceImage.isHidden = false;

    let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
    let imageUniqueName : Int64 = Int64(NSDate().timeIntervalSince1970 * 1000);
    let filePath = docDir.appendingPathComponent("\(imageUniqueName).png");

    do{
        if let pngImageData = UIImagePNGRepresentation((info[UIImagePickerControllerOriginalImage] as? UIImage)!){
            try pngImageData.write(to : filePath , options : .atomic)
            currentExerciceDto.imageCustom = "\(imageUniqueName).png"
        }
    }catch{
        print("couldn't write image")
    }

}

为了获取我刚刚保存的图片,我使用了这个功能

public static func fetchData(nameImage : String) -> UIImage{

    let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
    let filePath = docDir.appendingPathComponent(nameImage);

    if FileManager.default.fileExists(atPath: filePath.path){
        if let containOfFilePath = UIImage(contentsOfFile : filePath.path){
            return containOfFilePath;
        }
    }
    return UIImage();

}