Commit 3a2189a1 authored by zhangzhe's avatar zhangzhe

新建笔记图片、文字

parent a4ee7562
...@@ -12,10 +12,19 @@ import Photos ...@@ -12,10 +12,19 @@ import Photos
class SHInputController: SHBaseViewController class SHInputController: SHBaseViewController
{ {
// MARK: - property // MARK: - property
// private var viewModel : SHInputViewModel = SHInputViewModel() /// 文件夹model
var folderModel : SHRecordFolderModel!
/// 保存成功回调
var saveSuccessCallBack:((SHRecordFolderModel)->Void)?
/// 存储数据的model /// 存储数据的model
private var dataModel : SHRecordModel = SHRecordModel() private var dataModel: SHRecordModel = SHRecordModel()
private var shInputView : SHInputView!
/// 未填写标题时的默认标题
private let defaultTitle: String = "新建笔记"
private var shInputView: SHInputView!
private let recorder_mp3: SHMp3RecordManager = SHMp3RecordManager.shared() private let recorder_mp3: SHMp3RecordManager = SHMp3RecordManager.shared()
...@@ -138,25 +147,53 @@ class SHInputController: SHBaseViewController ...@@ -138,25 +147,53 @@ class SHInputController: SHBaseViewController
// MARK: 点击navigationBar右侧按钮 // MARK: 点击navigationBar右侧按钮
@objc func didSelectedNavRightButton() @objc func didSelectedNavRightButton()
{ {
// self.dataModel.imageArray = SHInputViewModel.getImageArrayWithTextView(textView: self.shInputView.textView) self.dataModel.recordingType = 1
self.dataModel.createDate = Date()
self.dataModel.modifyDate = Date()
let imageArray = SHInputViewModel.setUpImageArrayWithTextView(textView: self.shInputView.textView)
self.dataModel.imagesPath = imageArray.imagesPathArray
self.dataModel.imagesIndex = imageArray.imagesIndexArray
if self.dataModel.rename.length == 0
{
self.dataModel.rename = defaultTitle
}
self.dataModel.txt = self.shInputView.textView.text.substring(self.dataModel.rename.length, length: self.shInputView.textView.text.length - self.dataModel.rename.length)! self.dataModel.txt = self.shInputView.textView.text.substring(self.dataModel.rename.length, length: self.shInputView.textView.text.length - self.dataModel.rename.length)!
// var dic : [String : Any] = Dictionary()
// dic.updateValue(self.viewModel.title, forKey: "title") // 保存到沙盒
// dic.updateValue(self.viewModel.content, forKey: "content")
// dic.updateValue(self.viewModel.imageArray, forKey: "imageArray")
//
// UserDefaults.standard.setValue(dic, forKey: "InputRecord")
// UserDefaults.standard.synchronize()
var recordList = CRUserDefaults.recordList var recordList = CRUserDefaults.recordList
let modelDictionary = getDictWith(obj: self.dataModel) var i = 0;
// var folderModel = SHRecordFolderModel()
CRUserDefaults.recordList = [modelDictionary] for dict in recordList!
{
var model = SHRecordFolderModel()
model = getDataDictWith(dict: dict)
if model.id == self.folderModel.id
{
model.dataSources.append(self.dataModel)
self.folderModel = model
break
}
i += 1
}
recordList![i] = getDictWith(obj: self.folderModel!)
CRUserDefaults.recordList = recordList
self.saveSuccessCallBack?(self.folderModel)
self.navigationController?.popViewController(animated: true) self.navigationController?.popViewController(animated: true)
} }
...@@ -302,17 +339,17 @@ extension SHInputController: UIImagePickerControllerDelegate, UINavigationContro ...@@ -302,17 +339,17 @@ extension SHInputController: UIImagePickerControllerDelegate, UINavigationContro
let newSize = CGSize(width: newWidth, height: newHeight) let newSize = CGSize(width: newWidth, height: newHeight)
// // 如果没有标题添加"未命名"作为标题 // 如果没有标题添加默认标题
// if self.viewModel.title.length == 0 if self.dataModel.rename.length == 0
// { {
// // 标题 // 标题
// let titleString = NSMutableAttributedString(string: "未命名", attributes: (SHInputViewModel.titleAttributes() as! [NSAttributedString.Key : Any])) let titleString = NSMutableAttributedString(string: self.defaultTitle, attributes: (SHInputViewModel.titleAttributes() as! [NSAttributedString.Key : Any]))
// self.shInputView.textView.attributedText = titleString self.shInputView.textView.attributedText = titleString
//
// self.shInputView.placeholder.isHidden = true self.shInputView.placeholder.isHidden = true
//
// self.viewModel.title = titleString.string self.dataModel.rename = titleString.string
// } }
// 如果占位文字未隐藏, 隐藏占位文字 // 如果占位文字未隐藏, 隐藏占位文字
self.shInputView.placeholder.isHidden = true self.shInputView.placeholder.isHidden = true
......
...@@ -54,14 +54,31 @@ class SHInputViewModel: NSObject { ...@@ -54,14 +54,31 @@ class SHInputViewModel: NSObject {
} }
// MARK: 获取textView中的图片, 保存到本地, 并生成数组 // MARK: 获取textView中的图片, 保存到本地, 并生成数组
func setUpImageArrayWithTextView(textView: UITextView) -> (imagesPathArray: Array<String>, imagesIndexArray: Array<Int>) class func setUpImageArrayWithTextView(textView: UITextView) -> (imagesPathArray: Array<String>, imagesIndexArray: Array<Int>)
{ {
/// 图片路径数组 /// 图片路径数组
var imagesPathArray = Array<String>() var imagesPathArray = Array<String>()
/// 图片位置数组 /// 图片位置数组
var imagesIndexArray = Array<Int>() var imagesIndexArray = Array<Int>()
/// 遍历textView文字中的NSTextAttachment // 创建文件夹
/// 文件夹路径
let subFile = Date().format("yyyyMMdd")
let time = Date.init().timeStamp
let filePath = DocumentPath + "/" + subFile + time
do
{
try FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: true, attributes: nil)
}
catch let error
{
print(error)
return (imagesPathArray, imagesIndexArray)
}
// 遍历textView文字中的NSTextAttachment
let attributedString = textView.attributedText let attributedString = textView.attributedText
attributedString?.enumerateAttribute(NSAttributedString.Key.attachment, in: NSRange(location: 0, length: attributedString!.length), options: NSAttributedString.EnumerationOptions.longestEffectiveRangeNotRequired, using: { (attributes, range, stop) in attributedString?.enumerateAttribute(NSAttributedString.Key.attachment, in: NSRange(location: 0, length: attributedString!.length), options: NSAttributedString.EnumerationOptions.longestEffectiveRangeNotRequired, using: { (attributes, range, stop) in
...@@ -76,30 +93,34 @@ class SHInputViewModel: NSObject { ...@@ -76,30 +93,34 @@ class SHInputViewModel: NSObject {
// 将图片保存在本地 // 将图片保存在本地
// 把图片转成Data类型 // 把图片转成Data类型
let imageData : Data = (imageAttachment.image?.pngData())! let imageData : Data = (imageAttachment.image?.jpegData(compressionQuality: 1.0))!
// 保存的本地路径 // 保存的本地路径
let subFile = Date().format("yyyyMMdd") let imagePath = filePath + "/" + "image.jpeg" + imagesPathArray.count.string
let time = Date.init().timeStamp
let imagePath = DocumentPath + "/" + subFile + time + "image.png" let imageUrl : URL = URL(fileURLWithPath: imagePath)
let imageUrl : URL = URL(string: imagePath)! var saveSucceed : Bool = Bool(true)
// 将图片保存到沙盒 // 将图片保存到沙盒
do do
{ {
try imageData.write(to: imageUrl) try imageData.write(to: imageUrl)
}
catch let error
{
saveSucceed = false
print(error)
}
if saveSucceed
{
// 保存成功后存储保存的路径 // 保存成功后存储保存的路径
imagesPathArray.append(imagePath) imagesPathArray.append(imagePath)
// 存储图片位置 // 存储图片位置
imagesIndexArray.append(range.location) imagesIndexArray.append(range.location)
} }
catch
{
}
} }
} }
...@@ -107,33 +128,4 @@ class SHInputViewModel: NSObject { ...@@ -107,33 +128,4 @@ class SHInputViewModel: NSObject {
return (imagesPathArray, imagesIndexArray) return (imagesPathArray, imagesIndexArray)
} }
class func setUpImageArrayWithTextView(textView : UITextView) -> Array<Any>
{
var imageArray = Array<Any>()
let attributedString = textView.attributedText
attributedString?.enumerateAttribute(NSAttributedString.Key.attachment, in: NSRange(location: 0, length: attributedString!.length), options: NSAttributedString.EnumerationOptions.longestEffectiveRangeNotRequired, using: { (attributes, range, stop) in
var imageDictionary : Dictionary<String, Any> = Dictionary()
if attributes != nil
{
let imageAttachment = attributes as! NSTextAttachment
if imageAttachment.image != nil
{
let imageData : Data = (imageAttachment.image?.jpegData(compressionQuality: 1.0))!
imageDictionary.updateValue(imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters), forKey: "imageData")
imageDictionary.updateValue(range.location, forKey: "imageLocation")
imageArray.append(imageDictionary)
}
}
})
return imageArray
}
} }
...@@ -332,7 +332,14 @@ extension SHRecordDetailsVC { ...@@ -332,7 +332,14 @@ extension SHRecordDetailsVC {
switch sender.tag { switch sender.tag {
case 200: case 200:
let input = SHInputController.init() let input = SHInputController.init()
input.folderModel = currentModel
self.navigationController?.pushViewController(input, animated: true) self.navigationController?.pushViewController(input, animated: true)
input.saveSuccessCallBack = { model in
self.currentModel = model
self.updateocloudUI()
}
break break
default: default:
let record = UIStoryboard.init(name: "Record", bundle: nil).instantiateViewController(withIdentifier: "SHRecordViewController") as! SHRecordViewController let record = UIStoryboard.init(name: "Record", bundle: nil).instantiateViewController(withIdentifier: "SHRecordViewController") as! SHRecordViewController
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment