[英]instaFilter Processor error - missing return in a function expected to return
I'm currently learning swift and I'm currently taking a class for it. 我目前正在学习Swift,目前正在上课。 We are told to write a code to apply filters to a sample picture to change a given intensity of a color on an image in its parameter and then return the modified image.
我们被告知编写代码以对示例图片应用滤镜,以更改其参数上图像的给定颜色强度,然后返回修改后的图像。
In the code that I have written, on the last couple lines, it states an error saying 在我编写的代码的最后几行中,它指出了一个错误,
missing return in a function expected to return 'UIImage'
预期返回“ UIImage”的函数中缺少返回
my class code: 我的班级代码:
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 class: 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)
}
}
}
When a method declaration is written with return value, you must make sure it is always return a value. 用返回值编写方法声明时,必须确保它总是返回值。
I believe your error is in the following method: 我相信您的错误出在以下方法中:
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
}
Notice : that the return
statement is in the for
loop, meaning the method might not always return a value. 注意 :
return
语句在for
循环中,这意味着该方法可能并不总是返回值。
Fix : Put the return statement under the curly brackets marked in the code. 修复 :将return语句放在代码中标记的大括号下。
Added : You must return a value, therefore, you must create one 补充 :您必须返回一个值,因此,您必须创建一个
Side note: Make sure to handle nil
旁注:确保处理
nil
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.