在图库中的 CollectionView 中显示图像
Show image in CollectionView from gallery
我想将从图库中选择的图像显示到 CollectionView 中的 ImageView 中。我该怎么做呢??
在普通 ImageView 中显示 selectedImage 工作正常。
下面是我的代码:
var selectedImage = UIImage ()
@IBAction func selectPics(_ sender: Any)
{
if self.picsCollectionView.isHidden == true
{
self.picsCollectionView.isHidden = false
self.imageView.isHidden = true
}
self.picker.allowsEditing = true
self.picker.sourceType = .photoLibrary
present(self.picker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info:[String: Any])
{
if let image = info[UIImagePickerControllerEditedImage] as? UIImage
{
selectedImage = image
}
else if let image = info[UIImagePickerControllerOriginalImage] as? UIImage
{
selectedImage = image
}
else
{
print("Something went wrong")
}
self.dismiss(animated: true, completion: nil)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! UploadCollectionViewCell
cell.cellImgView.image = selectedImage
return cell
}
找到答案。只需添加一行。
解决方法如下:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info:[String: Any])
{
if let image = info[UIImagePickerControllerEditedImage] as? UIImage
{
selectedImage = image
self.picsCollectionView.reloadData()
}
else if let image = info[UIImagePickerControllerOriginalImage] as? UIImage
{
selectedImage = image
self.picsCollectionView.reloadData()
}
else
{
print("Something went wrong")
}
self.dismiss(animated: true, completion: nil)
}
我想将从图库中选择的图像显示到 CollectionView 中的 ImageView 中。我该怎么做呢?? 在普通 ImageView 中显示 selectedImage 工作正常。
下面是我的代码:
var selectedImage = UIImage ()
@IBAction func selectPics(_ sender: Any)
{
if self.picsCollectionView.isHidden == true
{
self.picsCollectionView.isHidden = false
self.imageView.isHidden = true
}
self.picker.allowsEditing = true
self.picker.sourceType = .photoLibrary
present(self.picker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info:[String: Any])
{
if let image = info[UIImagePickerControllerEditedImage] as? UIImage
{
selectedImage = image
}
else if let image = info[UIImagePickerControllerOriginalImage] as? UIImage
{
selectedImage = image
}
else
{
print("Something went wrong")
}
self.dismiss(animated: true, completion: nil)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! UploadCollectionViewCell
cell.cellImgView.image = selectedImage
return cell
}
找到答案。只需添加一行。
解决方法如下:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info:[String: Any])
{
if let image = info[UIImagePickerControllerEditedImage] as? UIImage
{
selectedImage = image
self.picsCollectionView.reloadData()
}
else if let image = info[UIImagePickerControllerOriginalImage] as? UIImage
{
selectedImage = image
self.picsCollectionView.reloadData()
}
else
{
print("Something went wrong")
}
self.dismiss(animated: true, completion: nil)
}