【发布时间】:2019-03-20 12:51:56
【问题描述】:
我想在 UserDefaults 中保存一个 UIButton 图像,稍后在我的代码中使用一个键检索它。 UIImage 来自 UIPickerView 正在使用的元组。
let defaults = UserDefaults.standard
@IBOutlet weak var topCurrencyPicker: UIPickerView!
var topPickerOptions = [
(symbol: String, name: String, sign: String, flag: UIImage)]()
func createTopAndBottomCurrency() {
let topUserIndex = topCurrencyPicker.selectedRow(inComponent: 0)
let topUserChoice = money.currencyISOCode[topUserIndex]
userCurrencyChoice = Currencies(top: topUserChoice, bottom: bottomUserChoice)
// Save user pickerViews Choice
defaults.set(topUserIndex,
forKey: ExchangePreferencesVC.topExPickerKey)
defaults.set(userCurrencyChoice.top.sign,
forKey: ExchangePreferencesVC.topExSignKey)
defaults.set(userCurrencyChoice.top.symbol,
forKey: ExchangePreferencesVC.topExSymbolKey)
}
是否可以像保存字符串一样轻松地保存此 UIImage?
【问题讨论】:
-
您真的需要物理保存图像吗?保存名称或 URL 还不够吗?
-
如果需要,我可以通过它的 Assets.xcassets 名称保存它,只要我将图像放回我想要的位置。
-
这绝对是更好的选择。资产名称必须是唯一的。
标签: swift