Reputation: 12625
I want to apply Core Image filters to do various CG manipulations of UIImage objects in Swift on iOS 8, but I'm having some difficulty using CGBitmapContextCreate(), particularly with the CGBitmapInfo parameter, which I want to set to the Swift-compatible equivalent of the constant kCGImageAlphaPremultipliedLast used with Objective C.
I'd like some working examples of how to do these things.
Upvotes: 0
Views: 818
Reputation: 12625
Here's an example of how to perform a Core Image filter to a UIIimage...
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!)!
}
Upvotes: 0
Reputation: 12625
Here's an example of how to perform CG operations on an image by fist creating a CGBItmapContext...
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)!
}
Upvotes: 0