ios - NSFileManager Failing on create file -
here functions save 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 \(success)") }
and i'm calling this
let path = "/spotcheck_\(spot.name)_photo\(indx).jpg" photomanager.savephoto(with: path, image: photo)
when call create file returns false every time , photo not saved.
edit: problem second line needed documentdirectory instead of picturesdirectory. shown below:
let path = (nssearchpathfordirectoriesindomains(.documentdirectory, .userdomainmask, true))[0]
ios apps run in sandbox. can't write pictures folder.
if want add picture user's photo library use phphotolibrary
or uiimagewritetosavedphotosalbum
.
if want save image within own app store file in documents folder.
fyi - when using nssearchpathfordirectoriesindomains
in ios, useful directories are:
- librarydirectory
- documentdirectory
- cachesdirectory
- applicationsupportdirectory
all others useful in macos.
Comments
Post a Comment