NSFileManager Failing on create file - ios

Here are my functions to save an image.
static func getDirectoryPath() -> String {
let path = (NSSearchPathForDirectoriesInDomains(.picturesDirectory, .userDomainMask, true))[0]
return path
}
static func savePhoto(with path: String, image: UIImage) {
let fM = FileManager.default
let path = getDirectoryPath() + path
let data = UIImageJPEGRepresentation(image, 0.5)
let success = fM.createFile(atPath: path, contents: data, attributes: nil)
print("File creation was \(success)")
}
and then I'm calling it like this
let path = "/SpotCheck_\(spot.name)_Photo\(indx).jpg"
PhotoManager.savePhoto(with: path, image: photo)
When I call create file is returns false every time and the photo is not saved.
EDIT: The problem was the second line needed to be documentDirectory instead of picturesDirectory. As shown below:
let path = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true))[0]

iOS apps run in a sandbox. You can't write to the Pictures folder.
If you want to add a picture to the user's photo library then use PHPhotoLibrary or just UIImageWriteToSavedPhotosAlbum.
If you simply want to save the image within your own app then store the file in the Documents folder.
FYI - when using NSSearchPathForDirectoriesInDomains in iOS, the only useful directories are:
libraryDirectory
documentDirectory
cachesDirectory
applicationSupportDirectory
All the others are only useful in macOS.

Related

Storing and retrieving image using path to stored file as String

I'm using Realm and storing the file path to captured images as Strings. I want to retrieve them later for use in a tableView. Here's my code to store the path for each image:
func saveImage(imageName: String){
//create an instance of the FileManager
let fileManager = FileManager.default
//get the image path
thisImagePath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent(imageName)
//get the image taken with camera
let image = originalCapturedImage
//get the PNG data for this image
let data = UIImagePNGRepresentation(image!)
//store it in the document directory
fileManager.createFile(atPath: thisImagePath as String, contents: data, attributes: nil)
print("Picture path at assignment \n")
print(thisImagePath as Any)
}
And here's the code to retrieve the image:
...
var paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsPath = paths[0] //Get the docs directory
let filePath = URL(fileURLWithPath: documentsPath).appendingPathComponent(item.picPath).path
let image = UIImage(contentsOfFile: filePath)
print("Picture path at retrieval \n")
print(item.picPath as Any)
cell.imageWell.image = image
cell.imageWell.clipsToBounds = true
return cell
}
Here's the comparison of the file path at runtime:
Picture path at assignment
/var/mobile/Containers/Data/Application/0E9CACAD-C6B3-4F6C-B0DB-72C43AC722E1/Documents/1535219147
...
...
Picture path at retrieval
/var/mobile/Containers/Data/Application/0E9CACAD-C6B3-4F6C-B0DB-72C43AC722E1/Documents/1535219147
The paths appear identical to me, yet no image appears. I've searched all over SO, and at one point came across a mention of the use of the URL for the file path. Somehow, I lost track of that entry and haven't been able to find it again.
Any help would be greatly appreciated!
You can try with below code for writing the image to the file instead of creating file with content. try? data.write(to: URL(fileURLWithPath: documentsPath))
You can also refer below code for saving image.
class func saveImageToFileWithDirectory(_ imageData:Data, fileName:String, folderName : String? = nil) {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as NSArray
let documentsDirectory = paths.object(at: 0) as! NSString
let path = documentsDirectory.appendingPathComponent(folderName) as NSString
if !FileManager.default.fileExists(atPath: path as String) {
do {
try FileManager.default.createDirectory(atPath: path as String, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
print(error.localizedDescription);
}
}
let imagePath = path.appendingPathComponent(fileName)
if !FileManager.default.fileExists(atPath: imagePath as String) {
try? imageData.write(to: URL(fileURLWithPath: imagePath))
} }
Code for retrieving the image looks good. If you need help in that also, pls comment, I will post that also.
Hope this solves your issue.

ios FileManager losing stored data when rebuilding app

I'm trying to store images inside the app's document folder, so the user can retrieve them at any later time that they want to. This is my code to store them:
func store(_ image: UIImage) -> String {
let imageName = "\(Date().timeIntervalSince1970)"
let imagePath = "\(documentasPath)/\(imageName).png"
let imageData = UIImagePNGRepresentation(image)
fileManager.createFile(atPath: imagePath, contents: imageData, attributes: nil)
return imagePath
}
And this is my code to retrieve the image from the storage:
func retrieveImage(from path: String) -> UIImage? {
guard fileManager.fileExists(atPath: path) else {
return nil
}
return UIImage(contentsOfFile: path)
}
It seems to work fine, except when I rebuild the app from xcode. Then all of my stored images disappear (although all of the paths I stored that pointed to them are still present and correct).
Is this some behavior of the default file manager? And is there a way to avoid this from happening? I want the images to only be deleted either manually or when I uninstall the app.
Thanks
The problem is that you are storing an absolute path. You can't do that, because your app is sandboxed, which means (in part) that the URL of the Documents folder can change. Store just the document name, and each time you want to save to it or write from it, calculate the path to the Documents folder again and append the document name and use that result as your path.
Change to this
func store(_ image: UIImage) -> String {
let imageName = "\(Date().timeIntervalSince1970)"
let documentsUrl = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
var imagePath = documentsUrl.appendingPathComponent("\(imageName).png")
let imageData = UIImagePNGRepresentation(image)
fileManager.createFile(atPath: imagePath, contents: imageData, attributes: nil)
return imagePath
}
func retrieveImage(from path: String) -> UIImage? {
guard fileManager.fileExists(atPath: path) else {
return nil
}
return UIImage(contentsOfFile: path)
}

Image is not existing in document directory if I will run the app again using Swift 3

I am saving a camera image into my document directory. I am creating a document directory in my util class. Here below is my code :-
//Get Document Directory Path
func getDirectoryPath() -> String {
let paths = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("Phss")
return paths
}
//Create Directory
func createDirectory(){
let fileManager = FileManager.default
let paths = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("Phss")
if !fileManager.fileExists(atPath: paths){
try! fileManager.createDirectory(atPath: paths, withIntermediateDirectories: true, attributes: nil)
}
else{
Helper.sharedInstance.Print("Already dictionary created." as AnyObject)
}
}
After that I am saving image by name and some value (docAddedTime) and storing the path(imagePath) in my core data DB.
func saveImageDocumentDirectory(imageData : Data, docName : String) -> String {
let fileManager = FileManager.default
let imagePAth = (getDirectoryPath() as NSString).appendingPathComponent("\(String(describing: docAddedTime!) + "_" + "\(String(describing: docName))").png")
fileManager.createFile(atPath: imagePAth, contents: imageData, attributes: nil)
return imagePAth
}.
I am fetching image by image path which is saved in my local core data DB.
let fileManager = FileManager.default
let imagePAth = doc!.docPath! //Core Data DB Path of image
if fileManager.fileExists(atPath: imagePAth) {
imgView.image = UIImage(contentsOfFile: imagePAth
}
The problem is first time I am able to fetch image and is showing in my imageView but after that I will run the app again and I am trying to fetch image by this imagePath which is stored in my core data DB then it's not giving that file exist at this path.
Image is present at same Path but showing is not exits. I am not sure why this is happening.
Each time you are building the app from Xcode, new folder is being created and files will be in the new folder.
Do not rebuild the app just close the app from simulator and start the app again from simulator itself.
Complete Path of your file will be like:
file:///Users/username/Library/Developer/CoreSimulator/Devices/4FA96815-521B-4D84-B5C7-10697DE1908B/data/Containers/Data/Application/54CC678C-7E96-4DB6-83CC-6ECB506DC9BF/Documents/tmp.png
On the next build:
Application/54CC678C-7E96-4DB6-83CC-6ECB506DC9BF/Documents/
will be changed as:
Application/3F3093C6-2140-473B-8F99-A717AF162CDE/Documents/
where 3F3093C6-2140-473B-8F99-A717AF162CDE is created by Xcode not you
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = URL(fileURLWithPath: paths).appendingPathComponent("fileName")
You can try the above snippet. This one don't create a directory though, I would suggest first try to fetch straight from Documents directory, once its a success then create a directory and then store it.Let me know the exact scenario. What is the value of the path showing when running for the next time.

Save and get image from folder inside Documents folder on iOS wrong path

On application launch I'm creating a folder inside the Documents directory, if there is none there already. This works great!
I'm downloading some images and would like to save them to use them later.
My problem is that my code seems to store the files in a documents directory that is not the same that would be on the next app launch.
I know that since iOS8 the documents directory can change from launch to launch. So I'm always retrieving a path to the Documents folder. Could someone answer me why this code can't get the path to the image correctly?
func requestImage(let url: String,let isbn: String, numberInRow: Int){
var documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
documentsPath.appendContentsOf("/bookImages")
print("Image folder path is: \(documentsPath)")
///var/mobile/Containers/Data/Application/E6B66A15-F166-46FE-A577-9B0D911F5C92/Documents/bookImages
let pathComponent = isbn.stringByAppendingString(".jpg")
print("Suggested filename: \(pathComponent)") //1234567891234.jpg
let imagePath = documentsPath.stringByAppendingString("/\(pathComponent)")
print("Image to be saved at: \(imagePath)")
// /var/mobile/Containers/Data/Application/E6B66A15-F166-46FE-A577-9B0D911F5C92/Documents/bookImages/9788202350420.jpg
if (data != nil){
NSFileManager.defaultManager().createFileAtPath(imagePath, contents: data!, attributes: ["YES" : "NSURLIsExcludedFromBackupKey"])
self.books[numberInRow].setValue(pathComponent, forKey: "imageurl")
}
}
When I would like to display these images I have this in the view controller
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let imageFolderPath = documentsPath.stringByAppendingString("/bookImages")
if let image = bookData.valueForKey("imageurl") as? String
{
print("Imagepath: \(self.imageFolderPath)/\(image)")
// /var/mobile/Containers/Data/Application/DB1F6FE9-1071-41A6-9E87-2A3D32ECD2B9/Documents/bookImages/9788202350420.jpg
let imagePath = self.imagePath.stringByAppendingString("/\(image)")
reuseableCell.bookCover.image = UIImage(contentsOfFile: imagePath)
}
I removed a lot of code that was not relevant. Why can't the image be displayed be found?
If anyone understand the error code, it is here:
BOMStream BOMStreamWithFileAndSys(int, off_t, size_t, int, char *, BomSys *): read: No such file or directory
Edit:
On application launch I'm searching for files in the Documents/bookImages and the files are there.
let paths: NSArray = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true)
if let documentDirectory = paths.firstObject{
do{
var path = documentDirectory as! String
path.appendContentsOf("/bookImages")
let documents = try NSFileManager.defaultManager().contentsOfDirectoryAtPath(path)
for files in documents {
let urlForm = NSURL.fileURLWithPath((path) + "/" + files)
do{
try print("\(files): \(urlForm.resourceValuesForKeys([NSURLIsExcludedFromBackupKey])), with filepath: \(urlForm)")
//Prints out folder and files in the desired location
} catch let error as NSError{
print("Can't find key: \(error)")
}
}
}catch let error as NSError{
print("Can't retrieve contents: \(error)")
}
}
9788202350420.jpg: ["NSURLIsExcludedFromBackupKey": 0], with filepath: file:///var/mobile/Containers/Data/Application/DB7BA523-6F75-42CF-92E6- ED2AF171D1AA/Documents/bookImages/9788202350420.jpg
9788203193538.jpg: ["NSURLIsExcludedFromBackupKey": 0], with filepath: file:///var/mobile/Containers/Data/Application/DB7BA523-6F75-42CF-92E6-ED2AF171D1AA/Documents/bookImages/9788203193538.jpg
9788203254703.jpg: ["NSURLIsExcludedFromBackupKey": 0], with filepath: file:///var/mobile/Containers/Data/Application/DB7BA523-6F75-42CF-92E6-ED2AF171D1AA/Documents/bookImages/9788203254703.jpg
I suspect the issue is happening because you didn't created the bookImages folder in the document directory. NSFileManager won't create the directories or sub-directories automatically.
// Creating directory
do
{
try NSFileManager.defaultManager().createDirectoryAtPath(documentsPath, withIntermediateDirectories: true, attributes: nil)
}
catch let error as NSError
{
NSLog("\(error.localizedDescription)")
}
// Saving image
if (data != nil)
{
// Also it would be better to check the file creation status
let status = NSFileManager.defaultManager().createFileAtPath(imagePath, contents: data!, attributes: ["NSURLIsExcludedFromBackupKey" : "YES"])
if status
{
// File created
self.books[numberInRow].setValue(pathComponent, forKey: "imageurl")
}
else
{
// File creation failed, update your question on stack overflow, someone will surely help you to find the issue :)
}
}
You can initialize your path variables as global variables at the top of your class, then just modify them within your requestImage method.
Then when you want to retrieve those images, you can just use the same variable name to ensure that it is the exact same path.
EDIT*
I think you may need to be reading some NSData instead. You can use UIImagePNGRepresentation or UIImageJPEGRepresentation to write your file to the documents directory. I found a tutorial here

How to get array of UIImage, from folder, in Swift?

I have an ordinary Xcode project like this ...
notice there's a folder (it is an actual folder - not just a group) named "images". It contains 25 ".png" images.
All I want to do is make an array of UIimage with each of those images.
(Or even, an array of the image names or similar, that would be fine - then could load them UIImage(named:)
How do I get at that folder "images"?? What about a subfolder "images/cars"?
I tried something like this but it finds nothing...
override func viewDidLoad()
{
let imageArray = NSBundle.mainBundle().URLsForResourcesWithExtension(
"png", subdirectory: "images")
print("test...")
for n:NSURL in imageArray!
{ print("found ..." ,n) }
}
We we assume the images are in the app's resource bundle. If not you need to make sure that your images directory is listed in the "Copy Bundle Resources" in the "Build Phases" of the target.
EDIT
This is only going copy the images into the app bundle, if you require the folder to be copied to the app bundle per the code below then please use the follow StackOverflow question to set it up correctly.
This gives us an array of URL's that we can then use with UIImage(data:) and NSData(contentsOfURL:) to create the image when needed.
Get the bundle's resource path and append the image directory then get the contents of the directory.
if let path = NSBundle.mainBundle().resourcePath {
let imagePath = path + "/images"
let url = NSURL(fileURLWithPath: imagePath)
let fileManager = NSFileManager.defaultManager()
let properties = [NSURLLocalizedNameKey,
NSURLCreationDateKey, NSURLLocalizedTypeDescriptionKey]
do {
let imageURLs = try fileManager.contentsOfDirectoryAtURL(url, includingPropertiesForKeys: properties, options:NSDirectoryEnumerationOptions.SkipsHiddenFiles)
print("image URLs: \(imageURLs)")
// Create image from URL
var myImage = UIImage(data: NSData(contentsOfURL: imageURLs[0])!)
} catch let error1 as NSError {
print(error1.description)
}
}
You can follow these steps to download them:
Create a new folder in finder and add all images (or folder, ...
everything).
Change folder name + ".bundle" (for example: YourListImage ->
YourListImage.bundle).
Add folder to project.
Add FileManager extension:
extension FileManager {
func getListFileNameInBundle(bundlePath: String) -> [String] {
let fileManager = FileManager.default
let bundleURL = Bundle.main.bundleURL
let assetURL = bundleURL.appendingPathComponent(bundlePath)
do {
let contents = try fileManager.contentsOfDirectory(at: assetURL, includingPropertiesForKeys: [URLResourceKey.nameKey, URLResourceKey.isDirectoryKey], options: .skipsHiddenFiles)
return contents.map{$0.lastPathComponent}
}
catch {
return []
}
}
func getImageInBundle(bundlePath: String) -> UIImage? {
let bundleURL = Bundle.main.bundleURL
let assetURL = bundleURL.appendingPathComponent(bundlePath)
return UIImage.init(contentsOfFile: assetURL.relativePath)
}
}
Use:
let fm = FileManager.default
let listImageName = fm.getListFileNameInBundle(bundlePath: "YourListImage.bundle")
for imgName in listImageName {
let image = fm.getImageInBundle(bundlePath: "YourListImage.bundle/\(imgName)")
}
Please try the following:
You have to register your images to "Copy Bundle Resources".
You have to add filter module in main Bundle.
enter image description here
It is working well on my side. Maybe you can change filter from "jpg" format into "png" one.
I've tested on iOS 10.x later, Swift 3.0 and xcode 8.1 version.
I would advise against loading all of your images into an array at once. Images tend to be large and it's easy to run out of memory and crash.
Unless you absolutely have to have all the images in memory at once it's better to keep an array of paths or URLs and load the images one at a time as needed.
Assuming the folder full of images is in your app bundle, you can use the NSBundle method URLsForResourcesWithExtension:subdirectory: to get an array of NSURLs to all the files in your images subdirectory, either with a specific filetype, or ALL files (if you pass nil for the extension.)
Once you have an array of file urls you can map it to an array of paths if needed, and then map that to an array of images.
Swift 4
if let path = Bundle.main.resourcePath {
let imagePath = path + "/images"
let url = NSURL(fileURLWithPath: imagePath)
let fileManager = FileManager.default
let properties = [URLResourceKey.localizedNameKey,
URLResourceKey.creationDateKey,
URLResourceKey.localizedTypeDescriptionKey]
do {
let imageURLs = try fileManager.contentsOfDirectory(at: url as URL, includingPropertiesForKeys: properties, options:FileManager.DirectoryEnumerationOptions.skipsHiddenFiles)
print("image URLs: \(imageURLs)")
// Create image from URL
let firstImageURL = imageURLs[0]
let firstImageData = try Data(contentsOf: firstImageURL)
let firstImage = UIImage(data: firstImageData)
// Do something with first image
} catch let error as NSError {
print(error.description)
}
}

Resources