Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Jamie Barton
Jamie Barton
14,498 Points

UIButton setImage depending on UIImageView.image presence

Hi

I have added using Storyboard a UIImageView and UIButton. The UIButton is ontop of the UIImageView and set at a size of 270x270.

The UIButton triggers the UIImagePickerControllerDelegate and then places the image inside of the UIImageView and sets the UIButton alpha to 0.2

However, I'm within the viewDidAppear() method of my custom class NewEventViewController to check if the image is present, and if it is, setting the image of the UIButton to the white version instead of the normal grey image.

Below is the code so far but it doesn't seem to do anything.

override func viewDidAppear(animated: Bool) {
    if (coverImageArea.image == nil) {
        chooseCoverBtn.setImage(UIImage(named: "camera"), forState: .Normal)
    } else {
        chooseCoverBtn.setImage(UIImage(named: "cameraWhite"), forState: .Normal)
    }
}

Can someone point me in the right direction?

2 Answers

Robert Bojor
PLUS
Robert Bojor
Courses Plus Student 29,439 Points

Hi Jamie,

Have you tried using dispatch_async and changing the image inside the block? Something like the code below...

override func viewDidAppear(animated: Bool) {
    var imageName:String
    if (coverImageArea.image == nil) {
        imageName = "camera"
    } else {
        imageName = "cameraWhite"
    }
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        chooseCoverBtn.setImage(UIImage(named: imageName), forState: .Normal)
    })
}
Jamie Barton
Jamie Barton
14,498 Points

Hi Robert Bojor

Thanks for reminding me about using dispatch_async.

Would you recommend using the queue for similar tasks to setting images etc.. ?

Robert Bojor
Robert Bojor
Courses Plus Student 29,439 Points

Hi Jamie,

Frankly I have used it only when the updates were not working, just like in your case.

Jamie Barton
Jamie Barton
14,498 Points

I've resolved this myself.

I added it to the method below

func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
    coverImageArea.image = image

    backgroundCoverImageArea.image = image
    backgroundCoverImageArea.alpha = 0.15

    chooseCoverBtn.setImage(UIImage(named: "cameraWhite"), forState: .Normal)
    chooseCoverBtn.alpha = 0.5

    self.dismissViewControllerAnimated(true, completion: nil)
}