当我为其分配新图像时,我的应用中的图像不会更新。
func updateUI () {
nameField.text = founders[founderIndex].name
print("founders[founderindex].image",founders[founderIndex].image)
print("picture.image",picture.image!)
print(picture)
picture.image = founders[founderIndex].image
print("founders[founderindex].image ",founders[founderIndex].image)
print("picture.image",picture.image!)
}
此输出如下:
founders[founderindex].image <UIImage: 0x17409b710> size {1200, 1200} orientation 0 scale 1.000000
picture.image <UIImage: 0x17409b490> size {1200, 1200} orientation 0 scale 1.000000
<UIImageView: 0x100f14d20; frame = (125 3; 124 124); autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x174038da0>>
founders[founderindex].image <UIImage: 0x17409b710> size {1200, 1200} orientation 0 scale 1.000000
picture.image <UIImage: 0x17409b490> size {1200, 1200} orientation 0 scale 1.000000
founders[founderIndex].image
并未分配给picture.image
。我环顾四周,但是还没有能够从它初始化的图像中改变它。
答案 0 :(得分:1)
事实证明,问题在于初始化程序。当我在初始化程序中有一个图像时,它会缓存它并且只使用它。我不完全确定为什么。我最终在变量中保存了实际的资产名称并初始化了imageName(作为字符串而不是UIImage)并使用它来加载图像:
picture.image = UIImage(named:founders[founderIndex].imageName)
如果我初始化除空字符串之外的任何内容,它将执行与以前相同的操作并仅保留默认/初始化值。所以我想密钥不是在初始化器中分配任何默认值。