0

I have a view controller, where I have a UIImage and UIlabels. This UIimage - image loads from the server and and the UIlabel text gets the value from the fields defined in the database. Now, in some records we dont have Images on the server and are blank, but there will be always the UILabel values. So if we dont have image value, how can we remove the UIImage and bring the labels up (as seen in the screenshots).

In the first screenshot we have the image so thats fine but the second screenshot we only have text and want to bring the text up. Is there a easy way?

Also, if we have various size image in regards to height and width, so how can we get the actual size of the image loaded on the app from the server

First Screenshot

FirstImage.png

Second Screenshot

Second Screenshot

code:

   private func checkImage(_ imageString:String)
{
    self.imageUI.layoutIfNeeded()
    if imageString.characters.count >= 2
    {
        if imageString != "<null>"
        {
            let url = NSURL(string:"http://www.xyz.com.au/images/"+imageString)

            let data = NSData(contentsOf:url! as URL)
            if data != nil
            {
                imageUI.image = UIImage(data:data! as Data) //= UIImage(data:data!)
                //imageUI.sizeToFit()



                let data = NSData(contentsOf: url as! URL)
                let Image = UIImage(data: data as! Data)!

            DispatchQueue.main.async {
                if data != nil
                {
                    // We've received valid image.
                    // Find the height & Width of received image and update the image-view's height constraint.
                     imageUI.image = data!.size.height

                }
                else
                {
                    // Sorry,The received image is invalid,Need to hide Image View,So make the Image-view height constraint's constant to 0 which eventually hides it self and pulls that below label up!
                    imageUI.constraints = 0
                }
            }

            }

        }
        else
        {
            //logoImageView.image = UIImage(named: "error-404")
        }
    }
    else
    {
        //logoImageView.image = UIImage(named: "error-404")
    }
}

error image:

Kunal Parekh
  • 380
  • 6
  • 24

2 Answers2

1

You can just give it height constraint and set it to 0 (required priority ,disable it first, enable again when needed) if the image/label is nil, remember to make other involved constraint lower priority

Tj3n
  • 9,837
  • 2
  • 24
  • 35
  • is there any good example so that I get a clear idea how to code and understand well ? Thank alot tho!! :) – Kunal Parekh Dec 08 '16 at 03:59
  • You can check the answers in [here](http://stackoverflow.com/questions/38364555/auto-layout-how-to-hide-1-view-in-a-view-with-3-equal-width-views/38364787#38364787), kinda similar i suppose – Tj3n Dec 08 '16 at 04:03
  • technically your image view has a size of 0 when it has no image. Your problem lies there. And if your image is always gonna be the same (and therefore is the reason why the height is hardcoded), then you might as well include the image in the bundle to avoid loading every time – Gil Sand Dec 09 '16 at 13:23
1

Try this,

    // 1. Get a reference to your image view's height constraint from story board to View controller.
    // Assume "constraintImageViewHeight" is the object name of the IBOutlet of image view's height constraint.

    // 2. In Web service completion block,Check for received image is nil/null
    // Assume "imgRecieved" is the object name of UIImage converted from response received.

    DispatchQueue.main.async {
        if imgRecieved != nil
        {
            // We've received valid image.
            // Find the height & Width of received image and update the image-view's height constraint.
            constraintImageViewHeight.constant = imgRecieved!.size.height
        }
        else
        {
            // Sorry,The received image is invalid,Need to hide Image View,So make the Image-view height constraint's constant to 0 which eventually hides it self and pulls that below label up!
            constraintImageViewHeight.constant = 0
        }
    }

Check this out for more information.

Hope that helps!

Community
  • 1
  • 1
Naresh Reddy M
  • 1,096
  • 1
  • 10
  • 27
  • Where you are getting confused and could you please tell me what all the errors you are getting? – Naresh Reddy M Dec 12 '16 at 05:00
  • I have posted my code in my Query. I have replaced the words instead of **constraintImageViewHeight** my object name is **imageUI** and instead of ** imgRecieved** my name for the object is **data** – Kunal Parekh Dec 12 '16 at 05:17
  • again, I am not getting the properties for UIimage (constant) as defined by you in the code solution – Kunal Parekh Dec 12 '16 at 05:22
  • First of all,replacing imageUI.image = data!.size.height With imageUI.image = Image!.size.height Will solve your 1st error, Next,What is the height constraint name of Image-view? – Naresh Reddy M Dec 12 '16 at 06:19
  • Still having errors after replacing the code as suggested. This time the error is **cannot assign value of type GCFloat to type 'UIImage'** – Kunal Parekh Dec 12 '16 at 07:29
  • and how do i find the height constraints? I have done any constraints for the Image-View – Kunal Parekh Dec 12 '16 at 07:30
  • Please clarify me a couple of things,Firstly, 1. Have you given height constraint for your image view in storyboard? 2. If Yes,Have you taken reference(IBOutLet) to the height constraint in to your view controller? 3. If Yes,What's the name of Your height constraint reference in view controller? So that i can help you further. – Naresh Reddy M Dec 12 '16 at 07:37
  • Check how to take reference of a UI Object's constraint to view controller from below Thread's first answer with 30 up votes http://stackoverflow.com/questions/22086054/unable-to-make-outlet-connection-to-a-constraint-in-ib – Naresh Reddy M Dec 12 '16 at 07:39
  • Thanks @Naresh, your code exec without any errors, however, if the image NIL, the text under (Data from database) does not goes to the top. – Kunal Parekh Dec 12 '16 at 22:10
  • Have you given top spacing constraint from label to Image view? – Naresh Reddy M Dec 13 '16 at 05:11
  • Hi Naresh. I am new to this Swift 3 and not too sure how to use the tools efficiently. What if, I email you my code and could you help me. – Kunal Parekh Dec 13 '16 at 05:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130456/discussion-between-sarah-malik-and-naresh-reddy-m). – Kunal Parekh Dec 13 '16 at 05:31