问题描述
我们如何通过程序命名图像。例如,为下面生成的图像分配名称。我们可以使用它来区分图像和以编程方式绘制的其他图像的名称。
func drawOval (width: CGFloat,height: CGFloat,name: String) -> UIImage {
let renderer = UIGraphicsImageRenderer(size: CGSize(width: width,height: height))
let image = renderer.image { ctx in
let path = UIBezierPath(ovalIn: CGRect(x: 0,y: 0,width: width,height: height))
path.stroke()
}
// TO DO: Assign this image a name,for example "image01"
return image
}
解决方法
您可以在每个UIImageView
上使用tags。我不知道直接将标识符添加到UIImage
的方法,因为它是NSObject
而不是UIView
的子类。为了在Swift中为对象添加标签,该对象必须是某种视图。
要实现此目的,您可以在该函数之外保留一个变量,以跟踪当前标记,然后在函数中对其进行递增。例如:
var currentTag = 0
//Function now returns a UIImageView
func drawOval (width: CGFloat,height: CGFloat,name: String) -> UIImageView {
let renderer = UIGraphicsImageRenderer(size: CGSize(width: width,height: height))
let image = renderer.image { ctx in
let path = UIBezierPath(ovalIn: CGRect(x: 0,y: 0,width: width,height: height))
path.stroke()
}
let imageView = UIImageView(image: image)
imageView.tag = currentTag
currentTag += 1
return imageView
}
随后在您的代码中:
if (imageView.tag == 0) {
//Do something
}
//You can also use
let taggedImageView = viewWithTag(0)
编辑:如果要保存图像并通过可用的UIImage
初始化程序之一加载它们,可以将它们写入磁盘上的缓存文件夹,然后使用UIImage(pathToFile:)
进行检索:>
//This will store the images in the caches directory for your app,which
//the system can clear when the device is low on storage. It will not be
//cleared while your app is open,though.
func saveImageToCacheDynamically(image: UIImage,name: String) {
let paths = FileManager.default.urls(for: .cachesDirectory,in: .userDomainMask)
let localPath = paths[0].appendingPathComponent(“ImageCache”,isDirectory: true)
do {
if FileManager.default.fileExists(atPath: localPath.absoluteString) {
//Write the png data representation of the image to disk in plaintext format
try image.pngData().write(to: localPath.appendingPathComponent("\(name).txt"))
} else {
try FileManager.default.createDirectory(at: localPath,withIntermediateDirectories: true,attributes: nil)
//Write the png data representation of the image to disk in plaintext format
try image.pngData().write(to: localPath.appendingPathComponent("\(name).txt"))
}
} catch {
print("Error locally saving image: \(error.localizedDescription)")
}
//Later in your code...
let paths = FileManager.default.urls(for: .cachesDirectory,in: .userDomainMask)
let localPath = paths[0].appendingPathComponent("ImageCache/\(imageIdentifier).txt")
if FileManager.default.fileExists(atPath: localPath.absoluteString) {
var fileData: Data!
do {
try fileData = Data(contentsOf: localPath)
} catch {
print("Error reading image file: \(error.localizedDescription)")
}
let image = UIImage(data: fileData)
//Do something with image
} else {
print("Error: image does not exist")
}
,
我相信您正在寻找的是类似NSCache ...
您可以通过类似于以下内容的方式定义缓存:
let imageCache = NSCache<String,UIImage>()
然后您可以像这样将对象添加到缓存中,其中someKeyString是您所指的“名称”:
imageCache.setObject(someImage,forKey: someKeyString)
最后,您可以像这样从缓存中检索图像
imageCache.object(forKey: someKeyString)
我建议您使用扩展程序或类似的方法来维护对应用程序中各处缓存的引用。
**注意:
当内存空间不足,应用关闭等情况时,NSCache将被清除。请参见here
要获得更持久的存储,我建议使用UserDefaults,Apple将其描述为“用户默认数据库的接口,您可以在其中存储应用程序启动期间的键值对。”使用它来处理个人资料图像或不经常更改的事物。我还建议您调查核心数据