在 Swift 中显示系统图像(SF 符号)是否使用网络调用?
Does showing system image(SF symbols) use a networking call in Swift?
我正在 ios 中创建一个应用程序,我在其中使用 UITableView 和 UITableViewCell 从 api 加载图像。
由于 UITableView 重用了单元格,因此当我快速滚动时会出现旧图像。为了防止这种情况,我使用系统图像(SF符号)设置了默认图像。
我还使用缓存来存储图像的 URL。
一切正常,但现在我想起来了,我每次都发送网络请求来检索该 systemImage,这似乎非常低效,因为我使用缓存来减少第一次的总网络调用地方。
有没有办法解决这个问题,或者这是我必须做出的权衡?
代码如下。
//use default image from SF symbols
let defaulticon = UIImage(systemName: "photo")?.withTintColor(.gray, renderingMode: .alwaysOriginal)
DispatchQueue.main.async {
cell.mealImage.image = defaulticon
}
guard cell.meal?.strMealThumb != nil else {
print("Category Image doesn't exist")
return
}
//use cache
if let imageData = model.imagecache.object(forKey: cell.meal!.strMealThumb as NSString) {
print("using cache")
DispatchQueue.main.async {
cell.mealImage.image = imageData
}
}
else {
let url = URL(string: cell.meal!.strMealThumb)
let session = URLSession.shared.dataTask(with: url!) { data, response, error in
if error == nil && data != nil {
let image = UIImage(data: data!)
//self.model.imagecache[cell.meal!.strMealThumb] = image
self.model.imagecache.setObject(image!, forKey: cell.meal!.strMealThumb as NSString)
DispatchQueue.main.async {
cell.mealImage.image = image
}
}
}
session.resume()
}
}
重写 UITableViewCell
中的 prepareForReuse
方法并在此函数中添加代码以清除可能因先前使用单元格而持续存在的无用数据。在您的示例中,在此函数中分配默认图像以产生更好的结果。
您问的是:
I set a default image using a system image(SF symbols).
...
Everything works as it should but now I think of it I'm sending a network request to retrieve that systemImage each time which seems incredibly inefficient since I was using a cache in order to reduce the total network calls in the first place.
不,UIImage(systemName:)
不发出网络请求。它缓存图像本身,正如 documentation 所说:
This method checks the system caches for an image with the specified name and returns the variant of that image that is best suited for the main screen. If a matching image object is not already in the cache, this method creates the image from the specified system symbol image. The system may purge cached image data at any time to free up memory. Purging occurs only for images that are in the cache but are not currently being used.
FWIW,您可以根据经验验证这不会执行断开网络连接并尝试使用它的网络请求。您会发现它工作正常,即使在断开连接时也是如此。
FWIW,通过保留对该着色系统图像的引用并重新使用它,而不是获取缓存的系统图像并重新着色,性能增益非常小(不到一毫秒?)。但是性能提升可以忽略不计
我正在 ios 中创建一个应用程序,我在其中使用 UITableView 和 UITableViewCell 从 api 加载图像。
由于 UITableView 重用了单元格,因此当我快速滚动时会出现旧图像。为了防止这种情况,我使用系统图像(SF符号)设置了默认图像。
我还使用缓存来存储图像的 URL。
一切正常,但现在我想起来了,我每次都发送网络请求来检索该 systemImage,这似乎非常低效,因为我使用缓存来减少第一次的总网络调用地方。
有没有办法解决这个问题,或者这是我必须做出的权衡?
代码如下。
//use default image from SF symbols
let defaulticon = UIImage(systemName: "photo")?.withTintColor(.gray, renderingMode: .alwaysOriginal)
DispatchQueue.main.async {
cell.mealImage.image = defaulticon
}
guard cell.meal?.strMealThumb != nil else {
print("Category Image doesn't exist")
return
}
//use cache
if let imageData = model.imagecache.object(forKey: cell.meal!.strMealThumb as NSString) {
print("using cache")
DispatchQueue.main.async {
cell.mealImage.image = imageData
}
}
else {
let url = URL(string: cell.meal!.strMealThumb)
let session = URLSession.shared.dataTask(with: url!) { data, response, error in
if error == nil && data != nil {
let image = UIImage(data: data!)
//self.model.imagecache[cell.meal!.strMealThumb] = image
self.model.imagecache.setObject(image!, forKey: cell.meal!.strMealThumb as NSString)
DispatchQueue.main.async {
cell.mealImage.image = image
}
}
}
session.resume()
}
}
重写 UITableViewCell
中的 prepareForReuse
方法并在此函数中添加代码以清除可能因先前使用单元格而持续存在的无用数据。在您的示例中,在此函数中分配默认图像以产生更好的结果。
您问的是:
I set a default image using a system image(SF symbols).
...
Everything works as it should but now I think of it I'm sending a network request to retrieve that systemImage each time which seems incredibly inefficient since I was using a cache in order to reduce the total network calls in the first place.
不,UIImage(systemName:)
不发出网络请求。它缓存图像本身,正如 documentation 所说:
This method checks the system caches for an image with the specified name and returns the variant of that image that is best suited for the main screen. If a matching image object is not already in the cache, this method creates the image from the specified system symbol image. The system may purge cached image data at any time to free up memory. Purging occurs only for images that are in the cache but are not currently being used.
FWIW,您可以根据经验验证这不会执行断开网络连接并尝试使用它的网络请求。您会发现它工作正常,即使在断开连接时也是如此。
FWIW,通过保留对该着色系统图像的引用并重新使用它,而不是获取缓存的系统图像并重新着色,性能增益非常小(不到一毫秒?)。但是性能提升可以忽略不计