ImagePicker 的已编辑图像未显示在 ImageView 中 [英] Edited Image of ImagePicker is not being displayed in the ImageView

查看:36
本文介绍了ImagePicker 的已编辑图像未显示在 ImageView 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 ImagePicker 中的图像显示到名为 profileIG 的 ImageView.问题是在这里我可以使用 ImagePicker 并且它可以正确显示,但是我选择的编辑后的图像 UIImagePickerControllerEditedImage 没有显示在 imageView 中

I am trying to display image from ImagePicker to the ImageView named profileIG. Issue is here I am able to use ImagePicker and it is being presented properly but the edited image which I choosed UIImagePickerControllerEditedImage is not being displayed in the imageView

这里我的代码 EnrollCellCollectionViewCell.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

and this is my 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 是:-

Firstly u need to notify the collection view like you are adding new image by calling self.YOUR_COLLECTION_VIEW.reloadData() Like : - here pickedImage is:-

 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 更新,例如:-

then update at cellForItemAt indexPath like : -

 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 的已编辑图像未显示在 ImageView 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆