Importing a image to a tableViewController

Updated on November 15, 2017 in Swift 3
8 on November 5, 2017

Hey im trying to send a image from one viewcontroller that i upload from the library to a tableview but i have not yet succeded but maybe somone of you can help. 

this is what i have wrote to transfer the image i get no errors or anything and my text does work but the image does not i have even tryed using the “append” function but it just tells me that it is not a “String” and does not work with a image. maybe im doing something wrong please help thanks 🙂 

NOTE** there is a global variabel of “MyImage” in the tableviewcontroller script.

and iam using Swift 4

    @IBOutlet weak var addTitle: UITextField!

    

    @IBAction func doneEditing(_ sender: Any) {

        if (addTitle.text != “”){

            actys.append(addTitle.text!)

            addTitle.text = “”

  }

  newImage.image = MyImage

}    

@IBOutlet weak var newImage: UIImageView!

 
  • Liked by
Reply
7 on November 8, 2017

To help you I’d need more information. The Code snippet you provided only shows how you append a String to (most likely) an Array(?=actys). I haven’t created something like a Image creator with Text, but to do this you’d have to get an Image and create a second layer and put the Text above the Image and convert the String to an Image somehow, I’m sure it’s easy to find on the internet.

But from your text I understood that you want to get an Image from your Library and then add the Image to a TableView(Cell?). If you get the Image from another ViewController I’d suggest a “prepareForSegue”-Function to add the Image to the TableView(Cell) in the following TableViewController.

For the moment this is all I can do… I’d need more Code to see whats going wrong or whats missing. Cheers Ben

on November 8, 2017

thanks for the help, i have changed my code a bit and i requested help also on an other forum but here is a link to that maybe it will give you a better understanding on what im trying to accomplish 

here is a link: https://stackoverflow.com/questions/47134345/import-a-image-from-photolibrary-to-a-tableview

i did explain it better there 🙂 

sorry for my bad explenation.

on November 8, 2017

Yep, Lineesh explained it very well. Your Problem was that you have had a global variable of type UIImage. This variable contains only ONE Image and therefore all your Image Cells had the same Image. Simply make that variable into an Array of UIImages

var imageArray: [UIImage] = [ Insert Images in here ]

and then get each Image with [indexPath.row] in your function. Everything should work just fine then.

on November 8, 2017

Yep, Lineesh explained it very well. Your Problem was that you have had a global variable of type UIImage. This variable contains only ONE Image and therefore all your Image Cells had the same Image. Simply make that variable into an Array of UIImages

var imageArray: [UIImage] = [ Insert Images in here ]

and then get each Image with [indexPath.row] in your function. Everything should work just fine then.

The problem you had (” FATAL ERROR: index out of range “) is because you tried to iterate through the Array and the Array was either Empty or Out of Bounds:

var imageArray: [UIImage] = [ 1.jpg, 2.jpg, 3.jpg]

  • imageArray[0] = 1.jpg
  • imageArray[1] = 2.jpg
  • imageArray[2] = 3.jpg
  • imageArray[3] -> FATAL ERROR: index out of range! Because the Array is empty at this position

You want create as many Cells as you have images in your Array, make sure you only iterate through the filled positions of the Array. For more information about Arrays click here.

If you need more help, just ask, I’m not that busy at the moment 🙂

on November 10, 2017

thanks for your help i succeded to import my image and my text field to the array i think, but now when i load it up it work until i press the “Done” edititng button and i go back to my tableview and nothing happens, i made a new question on the stackoverflow forum about my array and got help with that but now my problem is nothing shows some guy in the told me to use tableview.reload() after i set the text and image to the array, i have tryed to use it on several tableview functions but nothing that i have tryed seem to work, here is a breif view of my script Here   

and i have implomented this to my doneEditing button. 

 

        addTitles = addTitle.text!

        addLocations = addLocation.text!

        newImages = newImage.image!

        

        let element = MyCellRows(image: newImages,

                                 title: addTitles,

                                 location: addLocations)

        

        myCellRows.insert(element, at: 0)

Note i get no errors everything looks fine.

do you know maybe why nothing shows up when i hit “done”?

thanks for your time! 🙂

on November 10, 2017

Hi Ben! 

ive Tryed to print out the Array and it doesnt seem like its even adding new content to my array, i dont know if i print it wrong but i used 

    print(myCellRows, "Saved")
/* prints this:

[Acty3.MyCellRows, Acty3.MyCellRows, Acty3.MyCellRows, Acty3.MyCellRows, Acty3.MyCellRows] Saved

2017-11-10 11:42:08.137099+0100 Acty3[13737:112967] [LayoutConstraints] Unable to simultaneously satisfy constraints.

Probably at least one of the constraints in the following list is one you don't want.

Try this:

(1) look at each constraint and try to figure out which you don't expect;

(2) find the code that added the unwanted constraint or constraints and fix it.

(

    "<NSLayoutConstraint:0x604000284560 UILabel:0x7fc5fdc0a060'America'.width == 10.7812*UILabel:0x7fc5fdc0a060'America'.height   (active)>",

    "<NSLayoutConstraint:0x6040002867c0 UILabel:0x7fc5fdc0a060'America'.trailing == UITableViewCellContentView:0x7fc5fdc53680.trailingMargin   (active)>",

    "<NSLayoutConstraint:0x6040002864a0 UITableViewCellContentView:0x7fc5fdc53680.bottomMargin == UILabel:0x7fc5fdc0a060'America'.bottom + 128   (active)>",

    "<NSLayoutConstraint:0x604000286450 UILabel:0x7fc5fdc0a060'America'.leading == UITableViewCellContentView:0x7fc5fdc53680.leadingMargin   (active)>",

    "<NSLayoutConstraint:0x604000285e10 UILabel:0x7fc5fdc0a060'America'.top == UITableViewCellContentView:0x7fc5fdc53680.topMargin + 18   (active)>",

    "<NSLayoutConstraint:0x60400028a140 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fc5fdc53680.height == 199.5   (active)>",

    "<NSLayoutConstraint:0x60400028d3e0 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7fc5fdc53680.width == 375   (active)>"

) */

it prints it like this, and the other 3 items i have in the array, but i put in test1 as the title but i couldnt find it.is it not impelemnted or am i doing something wrong?

and this printed out my array i already created and only its content i had writen and only the image i had already implemented, do you think maybe it dosnt stay in the array because i dont have a CoreData for the array or should it work anyway? im thinking, iam trying to send the data from one viewcontroller to an other and then implement it in the tableview, do you think this requiers CoreData?

on November 10, 2017

just look at the top row the rest is really unneassery but i wanted to show you that it dosnt implement my “test1” item

on November 15, 2017

I think I found the problem:

@IBAction func doneEditing(_ sender: Any) {

(_ sender: Any) is wrong. You have to use (_ sender: UIButton)! Change this and the buttonClick should work! I’ve had some problems when I used (_ sender: Any).

Show more replies
  • Liked by
Reply
Cancel
Loading more replies