已编辑的 ImagePicker 图像未显示在 ImageView 中
Edited Image of ImagePicker is not being displayed in the 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, green: 0.56, blue: 1.00, 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, green: 0.56, blue: 1.00, 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, left: 0, bottom: 0, 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
}
我正在尝试将图像从 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, green: 0.56, blue: 1.00, 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, green: 0.56, blue: 1.00, 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, left: 0, bottom: 0, 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
}