I can take a picture with phone save it on gallery, but I don't know how can stock the path of picture, so after I can retrieve it from gallery (I don't know yet how retrieve a picture by his path) to show it on my controller.
我可以用手机拍照保存在gallery中,但是我不知道如何存储图片的路径,所以我可以从gallery中取回(我还不知道如何通过他的路径取回图片),然后在我的控制器上显示。
I have this code which works fine. I can see my image saved on gallery but I don't know how to get his url, witch I want to save on coredata
我有这个运行良好的代码。我可以看到我的图像保存在画廊,但我不知道如何得到他的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)
}
}
I read that I can use PHPhotoLibrary, but it give me error that it can't cast to nsurl Here is what I tried
我读到我可以使用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;
})
}
1 个解决方案
#1
1
I found a solution here https://medium.com/@Dougly/persisting-image-data-locally-swift-3-8bae72673f8a but i edit it because i couldn't load the image after application was closed, here is my solution hope it will help someone :)
我在这里找到了一个解决方案https://medium.com/@Dougly/persisting-image- locally-swift-3-8bae72673f8a但是我编辑了它,因为我无法在应用程序关闭后加载图像,我的解决方案希望它能帮助某些人:)
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")
}
}
and to fetch the image that i just saved, i use this function
为了获取刚才保存的图像,我使用这个函数
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();
}
#1
1
I found a solution here https://medium.com/@Dougly/persisting-image-data-locally-swift-3-8bae72673f8a but i edit it because i couldn't load the image after application was closed, here is my solution hope it will help someone :)
我在这里找到了一个解决方案https://medium.com/@Dougly/persisting-image- locally-swift-3-8bae72673f8a但是我编辑了它,因为我无法在应用程序关闭后加载图像,我的解决方案希望它能帮助某些人:)
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")
}
}
and to fetch the image that i just saved, i use this function
为了获取刚才保存的图像,我使用这个函数
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();
}