我目前已通过以下方式将图像设置到 UIImageView 中:
art_image.image = UIImage(named:(artworkPin.title!))
其中 art_image 是 ImageView ,artworkPin.title 是指图像的名称。但是,我想将第二个图像添加到 UIImageView(如果存在)我想将其编程为
art_image.image = UIImage(named:(artworkPin.title + "1")?)
这行得通吗?在这种情况下,我会将第二个图像命名为第一个图像的名称,但末尾带有“1”。示例:如果 Photo1.jpeg 存在,“Photo.jpeg”和“Photo1.jpeg”都会出现在 ImageView 中。
感谢您的帮助。
最佳答案
我自己也遇到过类似的任务。我所做的是,我使用 UIImageView
的框架创建了一个 UIScrollView
,其 contentSize
将是 imageView.frame.size。宽度 * 图片数量
。
let scrollView = UIScrollView(frame: view.bounds)
view.addSubview(scrollView)
scrollView.contentSize = CGSize(width: scrollView.bounds.size.width * CGFloat(numberOfImages), height: scrollView.bounds.size.height))
for i in 0...<numberOfImages.count-1 {
let imageView = UIImageView(frame: CGRect(x: scrollView.bounds.size.width * CGFloat(i), y: scrollView.bounds.origin.y, width: scrollView.bounds.size.width, height: scrollView.bounds.size.height))
imageView.image = UIImage(named: "artwork" + "\(i)")
scrollView.addSubview(imageView)
}
如果需要,您可以使用计时器
将其设置为滚动动画。
scrollView.setContentOffset(scrollPoint, animated: true)
关于ios - 多个 UIImage View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41658841/