OStack程序员社区-中国程序员成长平台

标题: ios - instaFilter 处理器错误 - 预期返回的函数中缺少返回 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 16:57
标题: ios - instaFilter 处理器错误 - 预期返回的函数中缺少返回

我目前正在学习 swift,并且正在为此上课。我们被告知要编写代码以将滤镜应用于示例图片,以在其参数中更改图像上颜色的给定强度,然后返回修改后的图像。

在我编写的代码中,在最后几行,它指出了一个错误,说

missing return in a function expected to return 'UIImage'

我的类(class)代码:

import UIKit


let image = UIImage(named: "sample")!

class brightnessFilter {

func increaseContrast(image: UIImage) -> UIImage{
    var rgbaImage = RGBAImage(image: image)!

    let avgRed = 118
    let avgGreen = 98
    let avgBlue = 83

    for y in 0..<rgbaImage.height {
        for x in 0..<rgbaImage.width {
            let index = y * rgbaImage.width + x
            var pixel = rgbaImage.pixels[index]
            let redLum = Int(pixel.red) - avgRed
            let greenLum = Int(pixel.green) - avgGreen
            let blueLum = Int(pixel.blue) - avgBlue
            pixel.red = UInt8(max(min(255, avgRed + 2 * redLum), 0))
            pixel.blue = UInt8(max(min(255, avgBlue + 2 * blueLum), 0))
            pixel.green = UInt8(max(min(255, avgGreen + 2 * greenLum), 0))
            rgbaImage.pixels[index] = pixel

        }
    }
    let newImage1 = rgbaImage.toUIImage()!
    return (newImage1)
  }

}

let test = brightnessFilter()
let processedImg = test.increaseContrast(image)


class redFilter {


func increaseContrast(image: UIImage) -> UIImage{
    var rgbaImage = RGBAImage(image: image)!

    let avgRed = 118

    for y in 0..<rgbaImage.height {
        for x in 0..<rgbaImage.width {
            let index = y * rgbaImage.width + x
            var pixel = rgbaImage.pixels[index]
            let redDiff = Int(pixel.red) - avgRed
            if (redDiff > 0) {
                pixel.red = UInt8( max(0, min(255, avgRed + redDiff * 5)))
                rgbaImage.pixels[index] = pixel
            }
        }
        let newImage2 = rgbaImage.toUIImage()!
        return (newImage2)
    }
  }
}

let test2 = redFilter()

RGBA 类:

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)
    }
  }
}



Best Answer-推荐答案


当一个方法声明写有返回值时,你必须确保它总是返回一个值。

我相信你的错误在于以下方法:

func increaseContrast(image: UIImage) -> UIImage {
    var rgbaImage = RGBAImage(image: image)!

    let avgRed = 118
    let newImage2 = UIImage() //<-- Added         

    for y in 0..<rgbaImage.height {
        for x in 0..<rgbaImage.width {
            let index = y * rgbaImage.width + x
            var pixel = rgbaImage.pixels[index]
            let redDiff = Int(pixel.red) - avgRed
            if (redDiff > 0) {
                pixel.red = UInt8( max(0, min(255, avgRed + redDiff * 5)))
                rgbaImage.pixels[index] = pixel
            }
        }
        let newImage2 = rgbaImage.toUIImage()!
        return (newImage2) //<---NOTICE
    }// <---FIX
  }

注意:return 语句在 for 循环中,这意味着该方法可能并不总是返回值。

修复:将return语句放在代码中标注的大括号下。

添加:您必须返回一个值,因此,您必须创建一个

旁注:确保处理 nil

关于ios - instaFilter 处理器错误 - 预期返回的函数中缺少返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38320653/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4