如何获取相机拍摄的照片路径 ios swift
how to get path of picture taken by camera ios swift
我正在使用 imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo 委托方法来获取用户在照片库中选择的图像的 url。但是当我尝试获取图像的 URL相机拍摄它 return 零。
这是我的代码。
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)
}
}
图片保存到相册后,可以在响应回调中获取路径
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
}
您拍摄的图片尚未保存,还没有任何名称。您需要先保存图像,然后才能获取图像的路径。这就是为什么 returns nil
.
如果您 select 来自图片库的图片而不是用相机拍摄,您将获得路径。
Rashwan L Answer 也对我有用,我无法使用 UIImageToSavedPhotoAlbum 保存图像,因此编写了 CameraImageManager class 方法来保存、加载和获取 URL 以上传图像。您也可以将其作为 Image 和 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
}
}
我正在使用 imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo 委托方法来获取用户在照片库中选择的图像的 url。但是当我尝试获取图像的 URL相机拍摄它 return 零。 这是我的代码。
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)
}
}
图片保存到相册后,可以在响应回调中获取路径
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
}
您拍摄的图片尚未保存,还没有任何名称。您需要先保存图像,然后才能获取图像的路径。这就是为什么 returns nil
.
如果您 select 来自图片库的图片而不是用相机拍摄,您将获得路径。
Rashwan L Answer 也对我有用,我无法使用 UIImageToSavedPhotoAlbum 保存图像,因此编写了 CameraImageManager class 方法来保存、加载和获取 URL 以上传图像。您也可以将其作为 Image 和 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
}
}