我在同一VC中的2个不同代码部分的视图(在Storyboard中创建)中以编程方式创建了一些图像视图,但是我得到了不同的结果。 这是我的代码:
var images: Int?
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
switch images {
case 0:
imagesCount = countImages.noImage
case 1:
imageView1 = UIImageView(frame: CGRect(x: 0, y: 0, width: Int((viewForImages.frame.width)), height: Int((viewForImages.frame.height))))
imageView1.image = UIImage(data: imageData1!)
viewForImages.addSubview(imageView1)
case 2:
for view in self.viewForImages.subviews {
view.removeFromSuperview()
}
imageView1 = UIImageView(frame: CGRect(x: 0, y: 0, width: Int((viewForImages.frame.width) / 2) - 1, height: Int((viewForImages.frame.height))))
imageView1.image = UIImage(data: imageData1!)
viewForImages.addSubview(imageView1)
imageView2 = UIImageView(frame: CGRect(x: Int(imageView1.frame.width) + 1, y: 0, width: Int((viewForImages.frame.width) / 2), height: Int(viewForImages.frame.height)))
imageView2.image = UIImage(data: imageData2!)
viewForImages.addSubview(imageView2)
case 3:
for view in self.viewForImages.subviews {
view.removeFromSuperview()
}
imageView1 = UIImageView(frame: CGRect(x: 0, y: 0, width: Int((viewForImages.frame.width) / 2) - 1, height: Int((viewForImages.frame.height))))
imageView1.image = UIImage(data: imageData1!)
viewForImages.addSubview(imageView1)
imageView2 = UIImageView(frame: CGRect(x: Int((viewForImages.frame.width) / 2) + 1, y: 0, width: Int((viewForImages.frame.width) / 2), height: Int((viewForImages.frame.height) / 2) - 1))
imageView2.image = UIImage(data: imageData2!)
viewForImages.addSubview(imageView2)
imageView3 = UIImageView(frame: CGRect(x: Int((viewForImages.frame.width) / 2) + 1, y: Int(imageView2.frame.height) + 1, width: Int((viewForImages.frame.width) / 2), height: Int((viewForImages.frame.height)) / 2))
imageView3.image = UIImage(data: imageData3!)
viewForImages.addSubview(imageView3)
default:
break
}
}
上面的代码正确地显示了图像视图,但我也将这些图像保存在Core Data中,当我再次打开此视图时,然后在viewDidLoad()中,我添加了以下代码:
override func viewDidLoad() {
super.viewDidLoad()
self.imageView1 = UIImageView(frame: CGRect(x: 0, y: 0, width: Int((viewForImages.frame.width) / 2) - 1, height: Int((viewForImages.frame.height))))
if let data = entry.imageData1 {
self.imageView1.image = UIImage(data: data)
}
viewForImages.addSubview(imageView1)
self.imageView2 = UIImageView(frame: CGRect(x: Int((viewForImages.frame.width) / 2) + 1, y: 0, width: Int((viewForImages.frame.width) / 2), height: Int((viewForImages.frame.height) / 2) - 1))
if let data = entry.imageData2 {
self.imageView2.image = UIImage(data: data)
}
viewForImages.addSubview(imageView2)
self.imageView3 = UIImageView(frame: CGRect(x: Int((viewForImages.frame.width) / 2) + 1, y: Int(imageView2.frame.height) + 1, width: Int((viewForImages.frame.width) / 2), height: Int((viewForImages.frame.height)) / 2))
if let data = entry.imageData3 {
self.imageView3.image = UIImage(data: data)
}
viewForImages.addSubview(imageView3)
}
注意:viewForImages是我使用Storyboard创建的视图,entry是一个具有3个属性的实体:图像Data1,2,3,类型为Binary Data(NSData)。 现在,当此视图出现时,如果视图已经有图像,它将运行上面的代码,但第一个imageView1覆盖了大约80%的视图,而其他2个图像视图很少出现。为什么这样?
答案 0 :(得分:1)
尝试更改内容模式
imageView.contentMode = .ScaleAspectFill
答案 1 :(得分:1)
Ok, so I got it figured out.
In viewDiDLoad()
method, before the switch
statement, I added the following line and it now works.
self.viewForImages.layoutIfNeeded()