How do I save multiple UIImages in FileManager - ios

I'm saving the image set by the user in FileManager. Then, I gotta retrieve these images and populate a UITableView. However, how can I save multiple different images then recover it all and present on my UITableView?
The error I'm facing is that when I save the image, then I retrieve it I just got a single UIImage, instead of multiple different images. Even though I save another different image, it just got replaced instead of adding another one. So, doing this way I can't populate a UITableView, because I always get a single UIImage instead of multiple saved images.
This is the function to save the Images
func saveImage(image: UIImage) -> Bool {
let data = UIImage.pngData(image)
guard let directory = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) as NSURL else {
return false
}
do {
try data()?.write(to: (directory.appendingPathComponent("fileName2.png")?.absoluteURL)!)
return true
} catch {
print(error.localizedDescription)
return false
}
}
That's how I'm saving the UIImage
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[.originalImage] as? UIImage {
self.imagePreview.image = image
self.namePreview.text = self.textFieldNome.text
//Saving Image
let savingImage = saveImage(image: image)
print("\(savingImage)")
}
self.picker.dismiss(animated: true, completion: nil)
}
This is the function retrieve the images
func getSavedImage(named: String) -> UIImage? {
if let dir = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) {
return UIImage(contentsOfFile: URL(fileURLWithPath: dir.absoluteString).appendingPathComponent(named).path)
}
return nil
}
ViewController: That's how I'm trying to retrieve the Images
override func viewDidLoad() {
super.viewDidLoad()
if let image = getSavedImage(named: "fileName2.png") {
print(image)
}
}

Solution is here :)
import Photos
let imagesArray = [UIImage]()
for image in imagesArray{ //multiple image array
let objImage: UIImage = image
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAsset(from:
objImage)
}, completionHandler: { success, error in
if success {
// Saved
}else {
// failed
}
})
}

Related

Saving a reference to an image in camera roll to recall later in app?

I'm trying to let the user take or select an image in an ImagePickerController, and I want to save a reference (as efficiently as possible) in my app to recall when the app loads again. Is saving the image's file URL the best approach for this?
import UIKit
import SwiftUI
struct ImagePicker: UIViewControllerRepresentable {
var sourceType: UIImagePickerController.SourceType = .photoLibrary
#Binding var selectedImage: UIImage
#Environment(\.presentationMode) private var presentationMode
func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
let imagePicker = UIImagePickerController()
imagePicker.allowsEditing = false
imagePicker.sourceType = sourceType
imagePicker.delegate = context.coordinator
return imagePicker
}
func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>) {
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
final class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var parent: ImagePicker
init(_ parent: ImagePicker) {
self.parent = parent
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
parent.selectedImage = image
}
if let imgUrl = info[UIImagePickerController.InfoKey.imageURL] as? URL{
let imgName = imgUrl.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
let localPath = documentDirectory?.appending(imgName)
let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
let data = image.pngData()! as NSData
data.write(toFile: localPath!, atomically: true)
//let imageData = NSData(contentsOfFile: localPath!)!
let photoURL = URL.init(fileURLWithPath: localPath!)//NSURL(fileURLWithPath: localPath!)
print(photoURL)
//TODO save this url in my app as a reference to look up
}
parent.presentationMode.wrappedValue.dismiss()
}
}
}
URL of image of camera roll may change , better way is to save your image to filesystem in the app sandbox and the you can save given name or something to retrieve it when u needed back
//MARK: save and retrive Images
extension UIImage {
func saveImage(imageName: String) {
guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
let fileName = imageName
let fileURL = documentsDirectory.appendingPathComponent(fileName)
guard let data = self.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 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
}
static func removeImage(fileName: String){
guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
let fileURL = documentsDirectory.appendingPathComponent(fileName)
if FileManager.default.fileExists(atPath: fileURL.path) {
do {
try FileManager.default.removeItem(atPath: fileURL.path)
print("Removed image")
} catch let removeError {
print("couldn't remove file at path", removeError)
}
}
}
}
Usage
yourImage.saveImage(imageName: "imageNameToSave")//<-save
UIImage.loadImageFromDiskWith(fileName: "ImageNameToRetrive")//<-retrive
UIImage.removeImage(fileName: "ImageNameToRemove")//<-remove
Edit:
You can definitely do FileManager as well, it truly depends on how many files you will be saving, and to where you want to save them. If it is a measly 1 file, that doesn't need to be secured and is public to the app, UserDefaults is the way to go. If you want to add a bit more control of that file, FileManager would be the way to go.
UserDefaults is your way to go to store locally.
Store Image Data
func locallyStoreImgData(image: UIImage, key:String) {
if let pngRepresentation = image.pngData() {
UserDefaults.standard.set(pngRepresentation, forKey: key)
}
else {
//Was unable to create png representation
}
}
Retrieve Image Data
func obtainImg(key:String) -> UIImage? {
if let imageData = UserDefaults.standard.object(forKey: key) as? Data,
let image = UIImage(data: imageData) {
return image
}
return nil
}
Use Case
locallyStoreImgData(image: myImage, key: "myImageKey")
if let image = obtainImg(key: "myImageKey") {
//Do something with image
}
else {
//Was unable to recreate image
}

i did Choose an image from camera or gallery and how to save that image to display in other View?

I did everything of getting image from camera or gallery, But how to save that image in array or in realm data to display in other viewController.
How can i do it?
Program :-
//Camera to save image and display later
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
showImage.image = info[UIImagePickerController.InfoKey.originalImage]
imagePicker.dismiss(animated: true, completion: nil)
}
If you don't want to retain image in app and just want to save image to display in other view controller then you can just pass that image to other view controller.
let viewContrller = OtherViewController() //the view controller you need to pass image to
viewController.image = image //the image you need to pass to other view controller
navigationController(push: vc, animated: true)
To save image in document directory...
func saveImage(withName name: String) {
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
// create the destination file url to save your image
let fileURL = documentsDirectory.appendingPathComponent(fileName)
// get your UIImage jpeg data representation and check if the destination file url already exists
if let data = image.jpegData(compressionQuality: 1.0),
!FileManager.default.fileExists(atPath: fileURL.path) {
do {
// writes the image data to disk
try data.write(to: fileURL)
print("file saved")
} catch {
print("error saving file:", error)
}
}
}
So you can display the saved image in other view controller.
func getImageFromDocDir(named imgName: String) -> UIImage? {
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
// create the destination file url to save your image
let fileURL = documentsDirectory.appendingPathComponent(imgName)
if FileManager.default.fileExists(atPath: fileURL.path) {
do {
let imgData = try Data(contentsOf: fileURL)
return UIImage(data: imgData)
} catch {
print(error.localizedDescription)
}
}
return nil
}
You can save image as Data, let's say you have pickedImage.
let imageData = pickedImage.pngData()
save imageData to realm.
To get image
let savedImage = UIImage(data: imageData)

How to get image name from UIImagePickerController taken with Camera

This below code is working perfectly fine for images picked from gallery. But will not work if taken with Camera. I tried to save image into storage and read again, but I was unable to do that. So could any one help me in this? Thank you.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let referenceUrl = info[UIImagePickerControllerReferenceURL] as? NSURL, image = info[UIImagePickerControllerOriginalImage] as? UIImage {
let phAsset = PHAsset.fetchAssetsWithALAssetURLs([referenceUrl], options: nil).lastObject as! PHAsset
PHImageManager.defaultManager().requestImageDataForAsset(phAsset, options: PHImageRequestOptions(), resultHandler: { (imagedata, dataUTI, orientation, info) in
if info!.keys.contains(NSString(string: "PHImageFileURLKey")) {
let path = info![NSString(string: "PHImageFileURLKey")] as! NSURL
print("path q\(path)")
self.mImageUrl = path
self.mlocalPath = path.path
self.mImageExtension = path.pathExtension
self.mImageName = path.lastPathComponent!
print("mImageName q\(self.mImageName)")
}
})
}
dismissViewControllerAnimated(true, completion: nil)
}
Swift 5+
As the previous answers sugested, the image is not stored in gallery yet and hence no imageName. You need to store it in gallery. Use the below Helper class to save and get images from FileManager.
Thanks to this Answer
class CameraImageManager {
static let shared = CameraImageManager()
public 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)
}
}
public 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
}
public 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
}
}
Now, in your imagePickerController didFinishPickingMediaWithInfo callback function, this is how you can assign a name to an image and save it.
public func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
guard let image = info[.editedImage] as? UIImage else { return }
let imageName = "RDV_" + UUID().uuidString
CameraImageManager.shared.saveImage(imageName: imageName, image: image)
print("IMAGE NAME IS: ", imageName)
}
Hope It Helps.
You can use a notification with addObserver like this
ViewController A : where you want image to be changed, add this in viewDidLoad
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.methodOfImageChange(_:)), name:"ImageChanged", object: nil)
Add this method in ViewController A
func methodOfImageChange(notification: NSNotification){
let appStatus = notification.userInfo as? Dictionary<String,AnyObject>
// appStatus contains your image in "image" key
}
Now in didFinishPickingMediaWithInfo add this
let dictionary: [String:AnyObject] = [
"image" : (info[UIImagePickerControllerOriginalImage] as? UIImage)!,
]
NSNotificationCenter.defaultCenter().postNotificationName("ImageChanged", object: self, userInfo: dictionary)
picker .dismissViewControllerAnimated(true, completion: nil)
Hope this helps
The image isn't in the gallery yet, so I don't believe you have a name.
In my app the flow (via navigation controller) is:
Selection VC (choice of Camera or Photo Library) ->
UIImagePickerController ->
Edit VC (with back navigation and action button for - among others - saving to Photo Library)
If the user chooses Camera, they take a picture and the options are "Retake" or "Use Photo". Is they choose "Use Photo", they are in the Edit VC.
If they then choose to go back to the Select VC, the image is nowhere to be found.

How to save image from camera/photo library into an array that is to be displayed in a UICollectionView

I realise that this question may be a little broad, but basically all I want to do is use image picker to select a photo by using a camera or within the photo library, and saving that photo to an array. I already know how to populate a collection view with images from assets folder. Just really how to save user images to an array. How do I name each image?
Also, it is preferable if the array could be a global one, so that these images can be accessed elsewhere.
Thanks in advance for your help.
Why not simple use:
var myImages = [UIImage]()
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let yourPickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
myImages.append(yourPickedImage)
}
dismissViewControllerAnimated(true, completion: nil)
}
Edit:
Save Image to Disk:
func saveImage (image: UIImage, path: String ) -> Bool{
let jpgImageData = UIImageJPEGRepresentation(image, 1.0)
let result = jpgImageData!.writeToFile(path, atomically: true)
return result
}
func getDocumentsURL() -> NSURL {
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
return documentsURL
}
func fileInDocumentsDirectory(filename: String) -> String
let fileURL = getDocumentsURL().URLByAppendingPathComponent(filename)
return fileURL.path!
}
How to store
let imagePath = fileInDocumentsDirectory(myImageName)
saveImage(yourPickedImage, path: imagePath)
Read
func loadImageFromPath(path: String) -> UIImage? {
let image = UIImage(contentsOfFile: path)
return image
}

How to save an image picked from a UIImagePickerController in Swift?

I'm building an app where I let the user to pick an image from its photo library. I was using this code to save it.
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
imgPicker.dismissViewControllerAnimated(true, completion: nil)
NSUserDefaults.standardUserDefaults().setValue(image, forKey: "bgImage")
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
imgPicker.dismissViewControllerAnimated(true, completion: nil)
}
I later figured out that you can't save the image with NSUserDefaults, but you can save the picked image's path, save it and load it with "if let..."
But I don't know how to do this (find the path and save it). The idea is for the user to choose the view background image, kinda like how Whatsapp does.
If I'm wrong and you can't store the path, is there any easy way to save the picked image without using online servers?
You can save and retrieve it using NSUserDefaults:
//Save image
let img = UIImage() //Change to be from UIPicker
let data = UIImagePNGRepresentation(img)
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "myImageKey")
NSUserDefaults.standardUserDefaults().synchronize()
//Get image
if let imgData = NSUserDefaults.standardUserDefaults().objectForKey("myImageKey") as? NSData {
let retrievedImg = UIImage(data: imgData)
}
Or you can read / write to file, if you prefer:
//Save image
let img = UIImage() //Change to be from UIPicker
let data = UIImagePNGRepresentation(img)!
do {
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
try data.writeToFile("\(documentsPath)myImage", options: [])
} catch {
print("Error")
}
//Get image
do {
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let readData = try NSData(contentsOfFile: "\(documentsPath)myImage", options: [])
let retreivedImage = UIImage(data: readData)
}
catch {
print("Error")
}
Swift 3:
//Save image
let data = UIImagePNGRepresentation(pickedImage)!
do {
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
try data.write(to: URL(string: "\(documentsPath)/myImage")!, options: .atomic)
} catch {
print("Error")
}
//Get image
do {
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let readData = try Data(contentsOf: URL(string: "\(documentsPath)/myImage")!)
let retreivedImage = UIImage(data: readData)
} catch {
print("Error")
}
There are two ways, one that you are describing but that is quite messy, the one I would suggest you is to take the image, turn into image data and store it in your application locally like in a sqlite database or in an array in user default. However saving that much data in user default is not a good practise. So, I would go with store it locally and I won't do it with messy sqlite codes, rather I would do it with core data.
class AddImageViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#IBOutlet var popUpThePickerButton: UIButton! //click on this button to appear the imagePicker
#IBOutlet var addPictureButton: UIButton! //when this button clicked saveImage method is called and there you save the imageData wherever you want
let picker = UIImagePickerController()
var selectedImage : UIImage!
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// when popUpThePickerButton is clicked
#IBAction func selectPictureFromPhotos(sender: UIButton) {
picker.editing = false
picker.sourceType = .PhotoLibrary
presentViewController(picker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
self.selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
// then save this image data in your database and when you want to show that image you can just turn the image data to image again
#IBAction func saveImage(sender: UIButton) {
let imageData = UIImagePNGRepresentation(self.selectedImage)
//save this image data in database
}
}

Resources