1,420
社区成员
发帖
与我相关
我的任务
分享
//获取图片数据
let 图片数据:Data = UIImageJPEGRepresentation(图片框.image!, 1.0)!
//以时间作为文件名
let 当前时间:Date = Date()
let 时间格式 = DateFormatter()
时间格式.dateFormat = "yyy-MM-dd 'at' HH:mm:ss.SSS"
let 当前时间字符串 = 时间格式.string(from: 当前时间) as String
//综合路径 去除时间中的空格
let 图片路径:String = 应用主目录+"/"+deleteSpaces(当前时间字符串)+".jpg"
//写入图片
writeDataToEndOfFile(fileURL: 图片路径, contentToWrite:图片数据, create: true)
//此处继续写入相应信息到主数据txt里
//写入文件
func writeDataToEndOfFile(fileURL url1:String,contentToWrite fileData:Data,create createOrNot:Bool){
let fM:FileManager = FileManager.default
if( !fM.fileExists(atPath:url1) && createOrNot ){
fM.createFile(atPath: url1, contents: nil, attributes: nil)
}
let fUpdater:FileHandle = FileHandle(forUpdatingAtPath: url1)!
//写入新的
fUpdater.seekToEndOfFile()
fUpdater.write(fileData)
fUpdater.closeFile()
}
//读图,无文件时自动生成
func readImage(imageUrl url:String) -> UIImage? {
let fM:FileManager = FileManager.default
if( !fM.fileExists(atPath:url)){
print(!fM.fileExists(atPath:url))
fM.createFile(atPath: url, contents: nil, attributes: nil)
print(!fM.fileExists(atPath:url))
}
let fUpdater:FileHandle = FileHandle(forUpdatingAtPath: url)!
let imageData:Data = fUpdater.availableData
fUpdater.closeFile()
let image = UIImage(data:imageData)
return image
}