Swift 3 - 另存图片
Swift 3 - Save image alternative
我需要找到另一种方法来保存图像
let save = UserDefaults.standard
let imageData = UIImageJPEGRepresentation(Photo.image!, 1.0)
save.set(imageData, forKey: "Image")
save.synchronize()
if let imgData = save.object(forKey: "Image"){
let compressedJPGImage = UIImage(data: imgData as! Data)
}
并加载图像
let imgData = save.object(forKey: "Image")
let compressedJPGImage = UIImage(data: imgData as! Data)
Photo.image = compressedJPGImage
此方法的问题是我用 UserDefaults.standard
保存了很多其他值,因此同步时需要很长时间(5-10 分钟)。
不建议将图像等大文件保存到 UserDefaults。 UserDefaults 旨在保存非常小的数据,例如用户首选的应用主题颜色。也许一个合适的替代方法是将图像保存在文档目录中。这是一个允许您保存图像的函数:
func saveImage(image: UIImage) -> String {
let imageData = NSData(data: UIImagePNGRepresentation(image)!)
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let docs = paths[0] as NSString
let uuid = NSUUID().uuidString + ".png"
let fullPath = docs.appendingPathComponent(uuid)
_ = imageData.write(toFile: fullPath, atomically: true)
return uuid
}
以上函数将为您创建保存图像的名称。如果您更愿意指定要保存的图像的名称,则可以执行以下操作(但您将负责确保您指定的图像名称是唯一的):
func saveImage(image: UIImage, withName name: String) {
let imageData = NSData(data: UIImagePNGRepresentation(image)!)
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let docs = paths[0] as NSString
let name = name
let fullPath = docs.appendingPathComponent(name)
_ = imageData.write(toFile: fullPath, atomically: true)
}
要检索这些图像,您可以将图像名称传递给此函数:
func getImage(imageName: String) -> UIImage? {
var savedImage: UIImage?
if let imagePath = getFilePath(fileName: imageName) {
savedImage = UIImage(contentsOfFile: imagePath)
}
else {
savedImage = nil
}
return savedImage
}
依赖这个函数工作的:
func getFilePath(fileName: String) -> String? {
let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
var filePath: String?
let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
if paths.count > 0 {
let dirPath = paths[0] as NSString
filePath = dirPath.appendingPathComponent(fileName)
}
else {
filePath = nil
}
return filePath
}
这是一个示例,说明您现在如何保存图像而不是 UserDefaults。我正在保存我将调用的图像 "Image":
saveImage(image: Photo.image, withName name: "Image")
这是我如何检索保存的图像的示例:
if let theSavedImage = getImage(imageName: "Image") {
//I got the image
}
UserDefaults
是存储用户偏好等一小部分数据的地方。 UserDefaults
的 space 非常有限,而且速度可能会很慢。在你的情况下,你提到它是 5-10 分钟,但我对此表示怀疑。
如果您希望跨应用程序会话存储图像(持久存储),您应该考虑使用文件系统 (Application_Folder/Library/Cache/) 或 Core Data 框架。访问图像时,您将在此处获得更好的性能。
如果不需要持久化图像,需要为应用程序的单个会话存储图像,则应使用UIImage
class的imageNamed:
API ].这个 API 在内存中加载一次图像并将其保存在系统缓存中。对于所有连续访问,它仅引用缓存的图像。如果加载太多图像,这将增加系统缓存大小和应用程序的内存占用。另一个 API 是 imageWithContentsOfFile:
。与第一个 API 不同,此 API 将始终在内存中加载新图像实例。一旦图像实例被释放,内存将被释放,而第一个 API.
不是这种情况
我需要找到另一种方法来保存图像
let save = UserDefaults.standard
let imageData = UIImageJPEGRepresentation(Photo.image!, 1.0)
save.set(imageData, forKey: "Image")
save.synchronize()
if let imgData = save.object(forKey: "Image"){
let compressedJPGImage = UIImage(data: imgData as! Data)
}
并加载图像
let imgData = save.object(forKey: "Image")
let compressedJPGImage = UIImage(data: imgData as! Data)
Photo.image = compressedJPGImage
此方法的问题是我用 UserDefaults.standard
保存了很多其他值,因此同步时需要很长时间(5-10 分钟)。
不建议将图像等大文件保存到 UserDefaults。 UserDefaults 旨在保存非常小的数据,例如用户首选的应用主题颜色。也许一个合适的替代方法是将图像保存在文档目录中。这是一个允许您保存图像的函数:
func saveImage(image: UIImage) -> String {
let imageData = NSData(data: UIImagePNGRepresentation(image)!)
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let docs = paths[0] as NSString
let uuid = NSUUID().uuidString + ".png"
let fullPath = docs.appendingPathComponent(uuid)
_ = imageData.write(toFile: fullPath, atomically: true)
return uuid
}
以上函数将为您创建保存图像的名称。如果您更愿意指定要保存的图像的名称,则可以执行以下操作(但您将负责确保您指定的图像名称是唯一的):
func saveImage(image: UIImage, withName name: String) {
let imageData = NSData(data: UIImagePNGRepresentation(image)!)
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let docs = paths[0] as NSString
let name = name
let fullPath = docs.appendingPathComponent(name)
_ = imageData.write(toFile: fullPath, atomically: true)
}
要检索这些图像,您可以将图像名称传递给此函数:
func getImage(imageName: String) -> UIImage? {
var savedImage: UIImage?
if let imagePath = getFilePath(fileName: imageName) {
savedImage = UIImage(contentsOfFile: imagePath)
}
else {
savedImage = nil
}
return savedImage
}
依赖这个函数工作的:
func getFilePath(fileName: String) -> String? {
let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
var filePath: String?
let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
if paths.count > 0 {
let dirPath = paths[0] as NSString
filePath = dirPath.appendingPathComponent(fileName)
}
else {
filePath = nil
}
return filePath
}
这是一个示例,说明您现在如何保存图像而不是 UserDefaults。我正在保存我将调用的图像 "Image":
saveImage(image: Photo.image, withName name: "Image")
这是我如何检索保存的图像的示例:
if let theSavedImage = getImage(imageName: "Image") {
//I got the image
}
UserDefaults
是存储用户偏好等一小部分数据的地方。 UserDefaults
的 space 非常有限,而且速度可能会很慢。在你的情况下,你提到它是 5-10 分钟,但我对此表示怀疑。
如果您希望跨应用程序会话存储图像(持久存储),您应该考虑使用文件系统 (Application_Folder/Library/Cache/) 或 Core Data 框架。访问图像时,您将在此处获得更好的性能。
如果不需要持久化图像,需要为应用程序的单个会话存储图像,则应使用UIImage
class的imageNamed:
API ].这个 API 在内存中加载一次图像并将其保存在系统缓存中。对于所有连续访问,它仅引用缓存的图像。如果加载太多图像,这将增加系统缓存大小和应用程序的内存占用。另一个 API 是 imageWithContentsOfFile:
。与第一个 API 不同,此 API 将始终在内存中加载新图像实例。一旦图像实例被释放,内存将被释放,而第一个 API.