swift,每次在XCode重新点击run时上一次保存的图片都会消失

VoyagerIX 2017-01-26 12:02:02
存储图片的代码(此时图片已经在一个UIImageView里)(还有原谅我用中文命名变量)
//获取图片数据
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
}


在不在xcode上再次点run的情况下,图片是可以顺利从本地读取出来的
但每次只要再次点run,图片就消失了,再次尝试读取就会在创建fileHandle 的那一行出现 unexpected found nil when unwrapping an optional value 的报错

有点急,非常感谢!
...全文
1364 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
AirZilong 2017-09-08
  • 打赏
  • 举报
回复
不要强转,会出问题的
目生鱼 2017-03-29
  • 打赏
  • 举报
回复
不明白什么意思。xcode每次运行app的时候,都会重新生成随机的app路径。这个路径如果你想拿只能拿到本次运行的路径。
VoyagerIX 2017-03-04
  • 打赏
  • 举报
回复
对了,我获取应用主目录的代码: let 应用主目录 = NSHomeDirectory()+"/Documents"
VoyagerIX 2017-03-04
  • 打赏
  • 举报
回复
依然没有解决啊
Step up your iOS development with the power and wealth of features of Xcode 7 About This Book Updated for the latest Xcode 7 release, this is the first book that will show you how to use all the new features available in Xcode 7 to their fullest This guide to will get you up and running with the entire workflow to develop Apple apps without previous Xcode experience Packed with plenty of tips on how to use Xcode with Swift 2 and other existing Apple frameworks to develop robust apps at a rapid pace Who This Book Is For If you are a novice programmer who is familiar with the concepts of object-oriented programming, but have little to no knowledge of Swift, Xcode, or Apple’s Cocoa APIs, then this book is for you. Starting with an introduction to the basics of Xcode and the Swift programming language, you will learn all of the skills that are essential to build an app and submit it to the App Store What You Will Learn Get an introduction to Xcode and get to know how to navigate and use the tool Build playgrounds to learn and explore the environment Create an adaptive UI with the interface builder that will help your app to scale through a wide range of devices Understand Table, Collection, and Stack views and find out how they can be implemented Create custom components for your application through frameworks and live previews Exploit Xcode’s advanced features to elevate your debugging and testing capabilities Run your application on the physical device and submit it to the App store In Detail Apple’s Xcode technology is making the development curve smoother than it has ever been with its easy-to-develop features and enhancements. With the latest release of Xcode 7, Apple has also added great support for Swift development. This book will introduce you to all the new features of Xcode 7 and demonstrate how Swift programming can be much easier, faster, and simply better with Xcode! Even if you’re starting with just a little knowledge of Swift or Xcode, you will learn the basics of the language as well as the tool. You will then use this knowledge to create simple applications and will learn how to debug and optimize your code. At the end of this book, you would have learned enough to build, run and submit your very own application to the App Store. Style and approach This is a step-by-step guide, full of practical and real world examples, to help you get to grips with Xcode with ease. A demo project is also provided at the end of the book, where you can test your skills. Packt Publishing; February 2016 ISBN 9781785882173 Read online, or download in secure EPUB or secure PDF format Title: Xcode 7 Essentials Author: Brett Ohland; Jayant Varma

1,420

社区成员

发帖
与我相关
我的任务
社区描述
Swift,苹果于2014年WWDC(苹果开发者大会)发布的新开发语言,可与Objective-C共同运行于MAC OS和iOS平台,用于搭建基于苹果平台的应用程序。
swift 技术论坛(原bbs)
社区管理员
  • Swift
  • 网罗开发
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧