如何将Image从TableViewCell传递到UIImage

时间:2016-11-26 07:07:20

标签: swift xcode uitableview uiimage swift3

My segues我试图找出是否有办法让我在完全不同的视图控制器中将我选择的单元格中的图像传递给新的UIImageView。我不完全理解segue是如何工作的,经过数小时的考虑后我还没有完全找到解决方案。我已经能够在tableview中显示我的图像,但我想将所选图像发送到UIImageView以获得更大的图片视图。我目前有2个VC。第一个叫做TableView

import UIKit

class TableView: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {


    @IBAction func toImage(_ sender: Any) {
        performSegue(withIdentifier: "ImageView", sender: self)
    }

    // Properties
    var imagesDirectoryPath:String!
    var images:[UIImage]!
    var titles:[String]!

    @IBAction func choosePhoto(_ sender: AnyObject) {
        let imagePicker = UIImagePickerController()
        if UIImagePickerController.isSourceTypeAvailable(.camera){
            imagePicker.sourceType = .photoLibrary
            print("Library selected")
        }else{
            print("No cam here!")
        }

        present(imagePicker, animated: true, completion: nil)
        imagePicker.delegate = self

    }

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self

        self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier:"CellID")

        images = []

        let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
        // Get the Document directory path
        let documentDirectorPath:String = paths[0]
        // Create a new path for the new images folder
        imagesDirectoryPath = documentDirectorPath + "/ImagePicker"
        var objcBool:ObjCBool = true
        let isExist = FileManager.default.fileExists(atPath: imagesDirectoryPath, isDirectory: &objcBool)
        // If the folder with the given path doesn't exist already, create it
        if isExist == false{
            do{
                try FileManager.default.createDirectory(atPath: imagesDirectoryPath, withIntermediateDirectories: true, attributes: nil)
            }catch{
                print("Something went wrong while creating a new folder")
            }
        }
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func refreshTable(){
        do{
            images.removeAll()
            titles = try FileManager.default.contentsOfDirectory(atPath: imagesDirectoryPath)
            for image in titles{
                let data = FileManager.default.contents(atPath: imagesDirectoryPath + "/\(image)")
                let image = UIImage(data: data!)
                images.append(image!)
            }
            self.tableView.reloadData()
        }catch{
            print("Error")
        }
    }


    //MARK: UIImagePickerControllerDelegate Protocol
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

        // Save image to Document directory
        var imagePath = Date().description
        imagePath = imagePath.replacingOccurrences(of: " ", with: "")
        imagePath = imagesDirectoryPath + "/\(imagePath).png" //this would change depending on the name of the image
        let data = UIImagePNGRepresentation(image)
        _ = FileManager.default.createFile(atPath: imagePath, contents: data, attributes: nil)
        dismiss(animated: true) { () -> Void in
            self.refreshTable()
        }
    }

    //MARK: UITableView DataSource
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: "CellID")

        cell?.imageView?.image = images[indexPath.row]
        cell.textLabel?.text = titles[indexPath.row]
        return cell
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return images.count
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "ImageView", sender: images[indexPath.row])
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        var secondVC : ImageView = segue.destination as! ImageView

        //secondVC.fullView = imagesDirectoryPath[IndexPath.row]
    }



}

这是我的ImageView,我希望我的图片来自单元格。

import UIKit

class ImageView:ViewController{

    @IBOutlet var fullView: UIImageView!
    var selectedImage = UIImage()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.fullView.image = self.selectedImage //Assign selected Image here




    }
    //segue via a button
    @IBAction func toTable(_ sender: Any) {
        performSegue(withIdentifier: "TableView", sender: self)

    }


}

那么有什么方法可以将现有图像传递给另一个VC中的UIImageView。我梳理了整个谷歌,但我似乎无法搞清楚。

谢谢!

1 个答案:

答案 0 :(得分:0)

在您的故事板文件中,确保自定义类在segue目标的笔尖上设置为ng-repeat="word in searchWords" ng-click="addWordToSet(word)" ng-class="isInside(word)" ng-style="{'background-color':color}" }"

enter image description here