1

I hope you can help me with this one.

I am trying to download an image from a url and the set it to a label. I am able to show it in a imageView but not a label.

This is my code.

func updateImage() {

    let ref = Database.database().reference()
    let uid = Auth.auth().currentUser?.uid
    let usersRef = ref.child("users").child(uid!)

    // only need to fetch once so use single event

       Database.database().reference().child("Products").queryOrderedByKey().observe(.childAdded, with: { snapshot in

        if !snapshot.exists() { return }

        //print(snapshot)

        let userInfo = snapshot.value as! NSDictionary
        print(userInfo)
        print(userInfo["name"]!)
        let profileUrl = userInfo["photoURL"] as! String

        print(profileUrl)
        let storageRef = Storage.storage().reference(forURL: profileUrl)
        storageRef.downloadURL(completion: { (url, error) in
            do {
                let data = try Data(contentsOf: url!)
                let image = UIImage(data: data as Data)
                self.productPhoto.image = image

            }
            catch _ {
                // Error handling
            }

        })
    })
}

I have tried many things like this

self.swipeLabel.backgroundColor = UIColor(patternImage: UIImage(named: image))

Thank you in advance

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • where did you call `self.swipeLabel.backgroundColor = UIColor(patternImage: UIImage(named: image))`? – trungduc Nov 08 '17 at 02:10
  • 1
    What are you actually trying to achieve? Labels can be used to some text, not an image. If you just want to set the picture as the background behind a text, you should still use a `UIImageView` and put a `UILabel` with a transparent background in front of that imageview. – Dávid Pásztor Nov 08 '17 at 02:11
  • 1
    I switched by self.productPhoto.image = image – Keneth Walters Nov 08 '17 at 02:12
  • 2
    @KenethWalters actually, i don't know why you want to set image for label. As DávidPásztor said, label isn't used to contain an image. – trungduc Nov 08 '17 at 02:15
  • As @trungduc suggested, You need to use imageView to display image instead of UILabel. But still if you need to set image on UILabel, then you need to create AttributedString to set image, like this [answer](https://stackoverflow.com/questions/19318421/how-to-embed-small-icon-in-uilabel) – Surjeet Singh Nov 08 '17 at 04:55

1 Answers1

3

Try this as I think what you are trying to do is setting the Background image to UILabel. Let's say you have got image from data as:

let data = try Data(contentsOf: url!)
let image = UIImage(data: data as Data)

Now you can set this image to UILabel with text by using the Attributed string as following :

 let imageAttachment =  NSTextAttachment()
    imageAttachment.image = image
    //Set bound to reposition
    let imageOffsetY:CGFloat = -5.0;
    imageAttachment.bounds = CGRect(x: 0, y: imageOffsetY, width: imageAttachment.image!.size.width, height: imageAttachment.image!.size.height)
    //Create string with attachment
    let attachmentString = NSAttributedString(attachment: imageAttachment)
    //Initialize mutable string
    let completeText = NSMutableAttributedString(string: "")
    //Add image to mutable string
    completeText.append(attachmentString)
    //Add your text to mutable string
    let  textAfterIcon = NSMutableAttributedString(string: "Using attachment.bounds!")
    completeText.append(textAfterIcon)
    self.label.textAlignment = .center;
    self.label.attributedText = completeText;

or If you want to set image to background then you can try adding imageview as UILabel's subview or vice versa as following :

label.addSubview(imageView) or imageView.addSubview(label)

Also you can try this to directly set image to Background of the UILabel :

label.backgroundColor = UIColor(patternImage: UIImage(named: "backgroundImage")!)
Umang Loriya
  • 840
  • 8
  • 15