【发布时间】:2017-09-14 14:50:36
【问题描述】:
我为我的图像视图设置了一个占位符图像。用户可以将这些更改为他们图书馆中的照片。完成此操作后,他们可以将这些图像上传到数据库。用户可以上传单个图像或多个图像,无论哪种方式,它们都作为 [UIImage] 上传。但是,我不希望上传占位符图像。
我已经设法实现了这一点,但是以一种非常不优雅的方式。我首先将 UIImageView 子类化,添加一个名为 isSet 的属性并将我所有的图像视图设置为此类:
class ItemImageViewClass: UIImageView {
//keeps track of whether the image has changed from the placeholder image.
var isSet = Bool()
}
然后当用户选择图像后设置图像时,isSet 属性设置为 true。
要检查图像视图中的图像是否已从占位符图像更改(即isSet == true),我使用了以下代码:
var imageArray: [UIImage]? = nil
if imageViewOne.isSet {
guard let mainImage = imageViewOne.image else {return}
if (imageArray?.append(mainImage)) == nil {
imageArray = [mainImage]
}
}
if imageViewTwo.isSet {
guard let imageTwo = imageViewTwo.image else {return}
if (imageArray?.append(imageTwo)) == nil {
imageArray = [imageTwo]
}
}
guard imageArray != nil else {
print("imageArray is nil")
alertMessage("Hey!", message: "Please add some images!")
return
}
当至少一张图片被选中时,数组将被保存到数据库中。
这似乎是一种非常混乱的方式;子类化 UIImageView 并且必须使用一系列 if 语句检查每个图像是否已更改。有没有更优雅的方法来实现这一点?谢谢
【问题讨论】:
标签: ios swift uiimageview uiimage