如何解决ImagePicker 的已编辑图像未显示在 ImageView 中
我正在尝试将 ImagePicker 中的图像显示到名为 profileIG
的 ImageView。问题是我可以使用 ImagePicker 并且显示正确,但是我选择的编辑图像 UIImagePickerControllerEditedImage
没有显示在 imageView 中
这里是我的代码 EnrollCell
,它是 CollectionViewCell.Swift
import UIKit
import Firebase
protocol pickTheImageDelegate {
func pickTheImage()
}
class EnrollCell: UsersCell,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
var delegate : pickTheImageDelegate?
static let identifier = "EnrollKey"
var demoController : DemoApplicationController?
@IBAction func pickImage() {
delegate?.pickTheImage()
}
let profileIG : UIImageView = {
let imageView = UIImageView()
imageView.backgroundColor = UIColor(red: 0.12,green: 0.56,blue: 1.00,alpha: 1.00)
imageView.layer.cornerRadius=22
imageView.layer.masksToBounds=true
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.isUserInteractionEnabled = true
return imageView
}()
let setProfilePhoto: UIButton = {
let profile = UIButton()
profile.setTitle("Select Profile Photo",for: .normal)
profile.setTitleColor(UIColor(red: 0.12,alpha: 1.00),for: .normal)
profile.titleLabel?.font = UIFont(name: "HelveticaNeue-Bold",size: 16)
profile.translatesAutoresizingMaskIntoConstraints = false
return profile
}()
lazy var collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero,collectionViewLayout: layout)
cv.backgroundColor = .white
cv.delegate = self
return cv
}()
override func setupViews() {
addSubview(profileIG)
addSubview(setProfilePhoto)
setProfilePhoto.addTarget(demoController,action: #selector(DemoApplicationController.pickTheImage),for: UIControl.Event.touchUpInside)
addConstraintsWithFormat("H:|-150-[v0(100)]-150-|",views: profileIG)
addConstraintsWithFormat("V:|-50-[v0(100)]-16-|",views: profileIG)
addConstraintsWithFormat("H:|-105-[v0(\(frame.width/2))]|",views: setProfilePhoto)
addConstraintsWithFormat("V:|-150-[v0(35)]|",views: setProfilePhoto)
}
}
这是我的 CollectionViewController DemoApplicationController.Swift
import UIKit
import Firebase
class DemoApplicationController: UICollectionViewController,UICollectionViewDelegateFlowLayout{
var enroll : EnrollCell?
override func viewDidLoad() {
super.viewDidLoad()
//title Label
navigationController?.navigationBar.isTranslucent = false
let titleLabel = UILabel(frame: CGRect(x: 0,y: 0,width: view.frame.width - 250,height: view.frame.height))
titleLabel.text = "Demo Application"
titleLabel.font = UIFont(name: "HelveticaNeue-Bold",size: 20)
navigationItem.titleView = titleLabel
titleLabel.textColor = UIColor(red: 0.12,alpha: 1.00)
enroll?.delegate=self
setupCollectionView()
}
func setupCollectionView() {
if let flowLayout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.scrollDirection = .horizontal
flowLayout.minimumLinespacing = 0
}
collectionView?.backgroundColor = .white
collectionView.register(FeedCell.self,forCellWithReuseIdentifier: UsersCell.identifer)
collectionView?.register(EnrollCell.self,forCellWithReuseIdentifier: EnrollCell.identifier)
collectionView?.contentInset = UIEdgeInsets(top: 50,left: 0,bottom: 0,right: 0)
collectionView?.scrollIndicatorInsets = UIEdgeInsets(top: 50,right: 0)
collectionView.isPagingEnabled = true
}
override func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
return 2
}
override func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: UsersCell.identifer,for: indexPath)
if indexPath.item == 1 {
return collectionView.dequeueReusableCell(withReuseIdentifier: EnrollCell.identifier,for: indexPath)
}
return cell
}
func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeforItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width,height: view.frame.height - 60)
}
}
extension DemoApplicationController : UIImagePickerControllerDelegate,UINavigationControllerDelegate,pickTheImageDelegate{
@objc func pickTheImage() {
let vc = UIImagePickerController()
vc.sourceType = .photoLibrary
vc.delegate = self
vc.allowsEditing = true
present(vc,animated: true,completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediawithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let imagePicked = info[UIImagePickerController.InfoKey(rawValue: "UIImagePickerControllerEditedImage")] as? UIImage {
enroll?.profileIG.image=imagePicked
}
dismiss(animated: true,completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true,completion: nil)
}
}
解决方法
首先,您需要通过调用 self.YOUR_COLLECTION_VIEW.reloadData()
来通知集合视图,就像您正在添加新图像一样
像 : -
这里的 pickedImage
是:-
var pickedImage : UIImage = UIImage(named: "photo")!
func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let imagePicked = info[.originalImage] as? UIImage {
pickedImage = imagePicked
self.collectionView.reloadData()
}
dismiss(animated: true,completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true,completion: nil)
}
然后在 cellForItemAt indexPath
更新,例如:-
override func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: UsersCell.identifer,for: indexPath)
if indexPath.item == 1 {
let en = collectionView.dequeueReusableCell(withReuseIdentifier: EnrollCell.identifier,for: indexPath) as! EnrollCell
en.profileIG.image = pickedImage
return en
}
return cell
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。