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

esyap4oy  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(144)

我需要用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...
}
fd3cxomn

fd3cxomn1#

试试这个

@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

相关问题