oscarimonbox
6/4/2018 - 8:16 AM

Grabar imagen en directorio de aplicación

    func saveImageToDocuments(image: UIImage) -> String?{

        let date = NSDate();
        let imageName = String(date.timeIntervalSince1970).replacingOccurrences(of: ".", with: "")
        //let localPath = String(format: "%@/%@", AppConstants.Folders.ImageFolder,imageName)
        let localPath = self.documentDirectoryPath?.appendingPathComponent(AppConstants.Folders.ImageFolder);
        
        let fileManager = FileManager.default
        if !fileManager.fileExists(atPath: (localPath?.path)!) {
            do {
                try fileManager.createDirectory(atPath: (localPath?.path)!, withIntermediateDirectories: true, attributes: nil)
            } catch {
                NSLog("Couldn't create document directory")
            }
        }
        
        
        var imgPath = URL(fileURLWithPath: (localPath!.appendingPathComponent(imageName).absoluteString))
        imgPath = imgPath.appendingPathExtension("png");
        
        
        do {
            try UIImagePNGRepresentation(image)?.write(to: imgPath, options: .atomic)
            return imgPath.absoluteString;
        } catch {
            print(error.localizedDescription)
        }
        return nil;
    }