我有一个带有UIImageView作为标题的UICollectionView(名为AddPhotoHeader
)。背景设置为灰色,但是一旦在下一个控制器(名为PhotoSelectorController
)中选择了图片,就应该返回带有UIImageView中图片的原始UICollectionView。
这是在AddPhotoHeader
中:
var selectedImage: UIImage? {
didSet {
DispatchQueue.main.async {
self.imageView.image = (self.selectedImage)!
print("This is the selectedImage:", (self.selectedImage)!)
}
imageView.backgroundColor = .white
}
}
let imageView: UIImageView = {
let iv = UIImageView()
iv.backgroundColor = UIColor.rgb(red: 234, green: 246, blue: 246)
iv.contentMode = .scaleAspectFill
iv.clipsToBounds = true
return iv
}()
在PhotoSelectorController
中,是在这里获取照片的地方:
fileprivate func fetchPhotos() {
let allPhotos = PHAsset.fetchAssets(with: .image, options: assetsFetchOptions())
DispatchQueue.global(qos: .background).async {
allPhotos.enumerateObjects({ (asset, count, stop) in
let imageManager = PHImageManager.default()
let targetSize = CGSize(width: 200, height: 200)
let options = PHImageRequestOptions()
options.isSynchronous = true
imageManager.requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFit, options: options, resultHandler: { (image, info) in
if let image = image {
self.images.append(image)
self.assets.append(asset)
if self.selectedImage == nil {
self.selectedImage = image
}
}
if count == allPhotos.count - 1 {
DispatchQueue.main.async {
self.collectionView?.reloadData()
}
}
})
})
}
}
用户选择照片并单击“下一步”后,将调用此函数以将图像放入AddPhotoHeader
的UIImageView中。
@objc func handleNext() {
let addPhotoHeader = AddPhotoHeader()
addPhotoHeader.selectedImage = header?.photoImageView.image
navigationController?.popViewController(animated: true)
}
selectedImage
中的打印语句将打印出来:
This is the selectedImage: <UIImage: 0x6000028510a0>, {485, 482}
但是它只是停留为灰色矩形,AddPhotoHeader
的UIImageView中没有图像显示
我觉得我在这里没有明显的步骤,但是我只是无法弄清楚。希望你能帮助我!谢谢!