1

I need to control a transparency (or opacity) of my uploaded images (JPEG, PNG, TIFF, etc) with NSSlider. How I can do it? Here is a code written in AppDelegate.swift

@IBAction func importButton(sender: AnyObject) {

    let uploadFile: NSOpenPanel = NSOpenPanel()       
    uploadFile.allowsMultipleSelection = false
    uploadFile.canChooseFiles = true
    uploadFile.canChooseDirectories = false
    uploadFile.runModal()
    let chosenPicture = uploadFile.URL

    if(chosenPicture != nil) {           
        let pictureImport = NSImage(contentsOfURL: chosenPicture!)
        imageWell.image = pictureImport
    }
}

@IBAction func brightnessSlider(sender: AnyObject) {

    //var alpha = sender...
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220

1 Answers1

1

Try this

@IBAction func brightnessSlider(sender: NSSlider) {

    var alpha = sender.minValue + (sender.doubleValue-sender.minValue)/(sender.maxValue-sender.minValue)
    imageWell.alphaValue = CGFloat(alpha)


}

Calculation for alpha becomes simpler if your minValue is always 0:

var alpha = sender.doubleValue/sender.maxValue
Andriy Gordiychuk
  • 6,163
  • 1
  • 24
  • 59