使用swift类中的函数

时间:2015-12-07 17:32:27

标签: ios swift swift-playground

我一直在尝试编写代码以在swift中为图像应用过滤器。我在操场上尝试以下代码。它由于某种原因而一直失败,我相信这是因为我在函数的变量引用中搞砸了。我不知道我是否应该在初始化程序中使用inout来使值变为可变或类似的东西。请指出我在哪里犯了错误。代码没有给出任何错误,但它没有产生我想要的图像输出。

//: Playground - noun: a place where people can play

import UIKit

class imageProcessor{

var avgRed = 0
var avgBlue = 0
var avgGreen = 0
var avgPixelValue = 0
var rgbaImage:RGBAImage? = nil

func averagePixelValue ( input inputImage: RGBAImage ) -> ( Int , Int , Int , Int ){

    var totalRed = 0
    var totalBlue = 0
    var totalGreen = 0

    for y in 0..<inputImage.height{
        for x in 0..<inputImage.width{
            let index = y * inputImage.height + x
            let pixel = inputImage.pixels[index]

            totalBlue += Int(pixel.blue)
            totalRed += Int(pixel.red)
            totalGreen += Int(pixel.green)

        }
    }

    let totalPixels = inputImage.height * inputImage.width

    let avgRed = totalRed/totalPixels
    let avgBlue = totalBlue/totalPixels
    let avgGreen = totalGreen/totalPixels
    let avgValue = ( avgRed + avgGreen + avgBlue )/3

    return ( avgRed , avgGreen , avgBlue , avgValue )

}

init ( inputImage: RGBAImage ) {

    rgbaImage = inputImage
    ( avgRed , avgGreen , avgBlue , avgPixelValue ) = averagePixelValue(input: rgbaImage!)

}

func addColorTint (color:String , intensity: Int=1)->RGBAImage{
    for i in 0..<self.rgbaImage!.height{
        for j in 0..<self.rgbaImage!.width{
            var pixel = self.rgbaImage!.pixels[i*self.rgbaImage!.height + j]
            let avgPixel = (Int(pixel.red) + Int(pixel.green) + Int(pixel.blue))/3

            if ( avgPixel > avgValue ){
                switch color{
                case "red" :
                    self.rgbaImage!.pixels[i*self.rgbaImage!.height + j].red = UInt8(max(0,min(255,Int(pixel.red) * intensity)))
                case "blue":
                    self.rgbaImage!.pixels[i*self.rgbaImage!.height + j].blue = UInt8(max(0,min(255,Int(pixel.blue) * intensity)))
                case "green":
                    self.rgbaImage!.pixels[i*self.rgbaImage!.height + j].green = UInt8(max(0,min(255,Int(pixel.green) * intensity)))
                default:
                    print ("0")

                }
            }
        }
    }
    return self.rgbaImage!
}
}



let image = UIImage(named: "sample")

var rgbaImage = RGBAImage(image: image!)





//  Process the image!

var newInstance = imageProcessor(inputImage: rgbaImage!)

let newrgbaInstance = newInstance.addColorTint("red", intensity: 2)

let newImage = newrgbaInstance.toUIImage()
newImage

代码似乎运行正常,但它在操场输出面板(窗口右侧)中没有显示输出。关于我应该如何做这项工作的任何建议都非常感谢。当我将它用作一个简单的函数但不是作为一个类时,它也起作用。

此外,我正在使用的库/类包含在

下面
import UIKit

public struct Pixel {
    public var value: UInt32

    public var red: UInt8 {
        get {
            return UInt8(value & 0xFF)
        }
        set {
            value = UInt32(newValue) | (value & 0xFFFFFF00)
        }
    }

    public var green: UInt8 {
        get {
            return UInt8((value >> 8) & 0xFF)
        }
        set {
            value = (UInt32(newValue) << 8) | (value & 0xFFFF00FF)
        }
    }

    public var blue: UInt8 {
        get {
            return UInt8((value >> 16) & 0xFF)
        }
        set {
            value = (UInt32(newValue) << 16) | (value & 0xFF00FFFF)
        }
    }

    public var alpha: UInt8 {
        get {
            return UInt8((value >> 24) & 0xFF)
        }
        set {
            value = (UInt32(newValue) << 24) | (value & 0x00FFFFFF)
        }
    }
}

public struct RGBAImage {
    public var pixels: UnsafeMutableBufferPointer<Pixel>

    public var width: Int
    public var height: Int

    public init?(image: UIImage) {
        guard let cgImage = image.CGImage else { return nil }

        // Redraw image for correct pixel format
        let colorSpace = CGColorSpaceCreateDeviceRGB()

        var bitmapInfo: UInt32 = CGBitmapInfo.ByteOrder32Big.rawValue
        bitmapInfo |= CGImageAlphaInfo.PremultipliedLast.rawValue & CGBitmapInfo.AlphaInfoMask.rawValue

        width = Int(image.size.width)
        height = Int(image.size.height)
        let bytesPerRow = width * 4

        let imageData = UnsafeMutablePointer<Pixel>.alloc(width * height)

        guard let imageContext = CGBitmapContextCreate(imageData, width, height, 8, bytesPerRow, colorSpace, bitmapInfo) else { return nil }
        CGContextDrawImage(imageContext, CGRect(origin: CGPointZero, size: image.size), cgImage)

        pixels = UnsafeMutableBufferPointer<Pixel>(start: imageData, count: width * height)
    }

    public func toUIImage() -> UIImage? {
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        var bitmapInfo: UInt32 = CGBitmapInfo.ByteOrder32Big.rawValue
        bitmapInfo |= CGImageAlphaInfo.PremultipliedLast.rawValue & CGBitmapInfo.AlphaInfoMask.rawValue

        let bytesPerRow = width * 4

        let imageContext = CGBitmapContextCreateWithData(pixels.baseAddress, width, height, 8, bytesPerRow, colorSpace, bitmapInfo, nil, nil)

        guard let cgImage = CGBitmapContextCreateImage(imageContext) else {return nil}
        let image = UIImage(CGImage: cgImage)

        return image
    }
}

1 个答案:

答案 0 :(得分:0)

我发现了错误。我一直在使用一个未识别的变量avgValue,我将其重命名为avgPixelValue。