如何使用NSSlider通过Swift为NSImage类调整亮度?

时间:2016-08-03 21:22:51

标签: macos swift2 nsslider

我需要使用NSSlider控制上传图像(JPEG,PNG,TIFF等)的透明度(或不透明度)。我怎么能这样做? 这是用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...
}

1 个答案:

答案 0 :(得分:1)

试试这个

@IBAction func brightnessSlider(sender: NSSlider) {

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


}

如果minValue始终为0,则alpha的计算变得更简单:

var alpha = sender.doubleValue/sender.maxValue