我的 swift代码中有一个过滤器有问题:luminosityModifier,红色,绿色,蓝色和alpha工作的其他过滤器.如果我使用0作为luminosityModifier值,代码运行没有错误,但如果我尝试任何其他整数,我看到错误
执行被中断,原因是:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0).这是此代码的最后一行.
import UIKit let image = UIImage(named: "sample.png")! // Process the image! // The Filter class contains the definition of a simple RGBA variable class Filter{ var rgba = [UInt8](count:5, repeatedValue: 0) } // Here are the 5 filters that can later be selected. By modifying the RGBA values, I intened to serve the requirement to create modifiers to individual filters. let redFilter: Filter = Filter() redFilter.rgba[0] = 255 let greenFilter: Filter = Filter() greenFilter.rgba[1] = 45 let blueFilter: Filter = Filter() blueFilter.rgba[2] = 255 let alphaFilter: Filter = Filter() alphaFilter.rgba[3] = 50 // Set luminosity Modifier in desired percentage (%), should be <100 to avoid explosion due to internal rounding let luminosityModifier = Filter() luminosityModifier.rgba[4] = 20 class ImageProcessor{ var filterSequenceList: [String] = [] //This Dictionary is later used to accept strings to set the filter the processor will apply. var filtersAvailable: [String: Filter] = [ "redFilter": redFilter, "greenFilter": greenFilter, "blueFilter": blueFilter, "alphaFilter": alphaFilter, "luminosityModifier": luminosityModifier ] func addFilterToSequence(filterName: String){ filterSequenceList.append(filterName) } func applyFilters(image: UIImage) -> UIImage{ var filters: [Filter] = [] // A list if filters gets populated according to the array of strings entered. for name in filterSequenceList{ filters.append(filtersAvailable[name]!) } let rgbaImage = RGBAImage(image: image)! // Loop through each pixel for y in 0..<rgbaImage.height{ for x in 0..<rgbaImage.width{ let index = y * rgbaImage.width + x var pixel = rgbaImage.pixels[index] // Loop through each filter for filter in filters{ for value in 0...4 { // RGBA values with value 0 get ignored. This means that 1 needs to be used if you wand that specific value to be very low. if(filter.rgba[value] != 0 ){ switch value{ case 0: pixel.red = filter.rgba[value] rgbaImage.pixels[index] = pixel case 1: pixel.green = filter.rgba[value] rgbaImage.pixels[index] = pixel case 2: pixel.blue = filter.rgba[value] rgbaImage.pixels[index] = pixel case 3: pixel.alpha = filter.rgba[value] rgbaImage.pixels[index] = pixel case 4: let red = pixel.red let green = pixel.green let blue = pixel.blue let luminosityModifier = Double(filter.rgba[value]) let relativeluminosity = Double(red) * 0.2126 + Double(green) * 0.7152 + Double(blue) * 0.0722 let transformerRed = (relativeluminosity - Double(green) * 0.7152 - Double(blue) * 0.0722) / 0.2126 let transformerGreen = (relativeluminosity - Double(red) * 0.2126 - Double(blue) * 0.0722 ) / 0.7152 let transformerBlue = (relativeluminosity - Double(red) * 0.2126 - Double(green) * 0.7152 ) / 0.0722 pixel.red = UInt8(transformerRed * luminosityModifier / 100 ) pixel.green = UInt8(transformerGreen * luminosityModifier / 100) pixel.blue = UInt8(transformerBlue * luminosityModifier / 100) rgbaImage.pixels[index] = pixel default: print("No image changes") } } } } } } let newImage = rgbaImage.toUIImage()! return newImage } } //--------------------------------------- Start using the class here-------------------------------- var processor: ImageProcessor = ImageProcessor() // Use the addFilterToSequence function and pass in one of the strings mentioned below. Using a non-existing filter name will cause a runtime error // "redFilter" // "greenFilter" // "blueFilter" // "alphaFilter" // "luminosityModifier" //processor.addFilterToSequence("redFilter") //processor.addFilterToSequence("blueFilter") processor.addFilterToSequence("luminosityModifier") //processor.addFilterToSequence("greenFilter") //processor.addFilterToSequence("alphaFilter") processor.filterSequenceList processor.applyFilters(image)rgba [4]的索引(4)超出范围.只有四个元素(从零开始),第四个元素(alpha)在索引3处.
至于过滤器本身,我认为你的意思是使用CILuminosityBlendMode()(并删除试图完全设置其rgba的崩溃线).它没有其他参数;它只是链条末端的一个简单的图像输入补丁.除非你打算通过它的alpha通道弱地应用它(不确定它是否可行;试试看),然后只需将索引4更改为3,这样你就可以在rgba数组中设置alpha元素.