我想在iOS 8上应用Core Image过滤器在Swift中对UIImage对象进行各种CG操作,但是我在使用CGBitmapContextCreate()方面遇到了一些困难,特别是对于我希望设置为CGBitmapInfo参数的CGBitmapInfo参数与目标C一起使用的常量kCGImageAlphaPremultipliedLast的Swift兼容等价物。
我想了解如何做这些事情的一些实例。
答案 0 :(得分:0)
这是一个如何通过拳头创建CGBItmapContext对图像执行CG操作的示例...
func setImageMaskColor(srcImage: UIImage, color: UIColor) -> UIImage {
var bounds = CGRectMake(0, 0, srcImage.size.width, srcImage.size.height)
var colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue)
var ctx = CGBitmapContextCreate(nil,
CGImageGetWidth(srcImage.CGImage),
CGImageGetHeight(srcImage.CGImage),
CGImageGetBitsPerComponent(srcImage.CGImage),
CGImageGetBytesPerRow(srcImage.CGImage),
CGImageGetColorSpace(srcImage.CGImage),
bitmapInfo)!
CGContextDrawImage(ctx, bounds, srcImage.CGImage)
CGContextSetBlendMode(ctx, kCGBlendModeSourceAtop)
CGContextSetFillColorWithColor(ctx, color.CGColor)
CGContextFillRect(ctx, bounds);
var resultCGimage = CGBitmapContextCreateImage(ctx)
CGContextDrawImage(ctx, bounds, resultCGimage)
return UIImage(CGImage: resultCGimage)!
}
答案 1 :(得分:0)
以下是如何对UIImage执行核心图像过滤的示例...
func imageAdjustHue(srcImage: UIImage, hue: CGFloat) -> UIImage {
var colorParam = [ kCIInputAngleKey : hue ]
var ciimage = CIImage(CGImage: srcImage.CGImage)
let filteredImage = ciimage.imageByApplyingFilter("CIHueAdjust", withInputParameters: colorParam)
return UIImage(CIImage: filteredImage!)!
}