如何获得相机ios swift拍摄的照片路径

时间:2017-06-13 13:19:36

标签: ios swift uiimagepickercontroller

我正在使用imagePickerController(_ picker:UIImagePickerController,didFinishPickingMediaWithInfo委托方法获取用户在照片库中选择的图像的网址。但是当我尝试获取相机拍摄的图像的URL时,返回nill。 这是我的代码。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if (imagePicker.sourceType == UIImagePickerControllerSourceType.camera) {
            let data = UIImagePNGRepresentation(pickedImage)
            UIImageWriteToSavedPhotosAlbum(pickedImage, nil, nil, nil)

            if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
                let imageURL =  info[UIImagePickerControllerReferenceURL] as? NSURL
                print(imageURL)
            }
        }

3 个答案:

答案 0 :(得分:2)

您拍摄的图像未保存且尚未显示任何名称。您需要先保存图像,然后才能获得图像的路径。这就是它返回nil的原因。

如果您从图像库中选择图像而不是使用相机拍摄图像,则会获得路径。

答案 1 :(得分:1)

将图像保存到相册后,您可以获得响应回调中的路径

func saveImage(image: UIImage, completion: @escaping (Error?) -> ()) {
   UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(path:didFinishSavingWithError:contextInfo:)), nil)
}

@objc private func image(path: String, didFinishSavingWithError error: NSError?, contextInfo: UnsafeMutableRawPointer?) {
    debugPrint(path) // That's the path you want
}

答案 2 :(得分:0)

Rashwan L Answer也为我工作,我无法使用UIImageToSavedPhotoAlbum保存图像,因此编写了CameraImageManager类方法来保存,加载和获取上传图像的URL。您也可以将其作为图片和URL的扩展名

class CameraImageManager {

   static func saveImage(imageName: String, image: UIImage) {

        guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }

        let fileName = imageName
        let fileURL = documentsDirectory.appendingPathComponent(fileName)
        guard let data = image.jpegData(compressionQuality: 1) else { return }

        //Checks if file exists, removes it if so.
        if FileManager.default.fileExists(atPath: fileURL.path) {
            do {
                try FileManager.default.removeItem(atPath: fileURL.path)
                print("Removed old image")
            } catch let removeError {
                print("couldn't remove file at path", removeError)
            }

        }

        do {
            try data.write(to: fileURL)
        } catch let error {
            print("error saving file with error", error)
        }

    }

    static func getImagePathFromDiskWith(fileName: String) -> URL? {

        let documentDirectory = FileManager.SearchPathDirectory.documentDirectory

        let userDomainMask = FileManager.SearchPathDomainMask.userDomainMask
        let paths = NSSearchPathForDirectoriesInDomains(documentDirectory, userDomainMask, true)

        if let dirPath = paths.first {
            let imageUrl = URL(fileURLWithPath: dirPath).appendingPathComponent(fileName)
            return imageUrl
        }

        return nil
    }

   static func loadImageFromDiskWith(fileName: String) -> UIImage? {

        let documentDirectory = FileManager.SearchPathDirectory.documentDirectory

        let userDomainMask = FileManager.SearchPathDomainMask.userDomainMask
        let paths = NSSearchPathForDirectoriesInDomains(documentDirectory, userDomainMask, true)

        if let dirPath = paths.first {
            let imageUrl = URL(fileURLWithPath: dirPath).appendingPathComponent(fileName)
            let image = UIImage(contentsOfFile: imageUrl.path)
            return image

        }

        return nil
    }

}