4
A
回答
3
- (UIImage *)negativeImage
{
// get width and height as integers, since we'll be using them as
// array subscripts, etc, and this'll save a whole lot of casting
CGSize size = self.size;
int width = size.width;
int height = size.height;
// Create a suitable RGB+alpha bitmap context in BGRA colour space
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *memoryPool = (unsigned char *)calloc(width*height*4, 1);
CGContextRef context = CGBitmapContextCreate(memoryPool, width, height, 8, width * 4, colourSpace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colourSpace);
// draw the current image to the newly created context
CGContextDrawImage(context, CGRectMake(0, 0, width, height), [self CGImage]);
// run through every pixel, a scan line at a time...
for(int y = 0; y < height; y++)
{
// get a pointer to the start of this scan line
unsigned char *linePointer = &memoryPool[y * width * 4];
// step through the pixels one by one...
for(int x = 0; x < width; x++)
{
// get RGB values. We're dealing with premultiplied alpha
// here, so we need to divide by the alpha channel (if it
// isn't zero, of course) to get uninflected RGB. We
// multiply by 255 to keep precision while still using
// integers
int r, g, b;
if(linePointer[3])
{
r = linePointer[0] * 255/linePointer[3];
g = linePointer[1] * 255/linePointer[3];
b = linePointer[2] * 255/linePointer[3];
}
else
r = g = b = 0;
// perform the colour inversion
r = 255 - r;
g = 255 - g;
b = 255 - b;
if ((r+g+b)/(3*255) == 0)
{
linePointer[0] = linePointer[1] = linePointer[2] = 0;
linePointer[3] = 0;
}
else
{
// multiply by alpha again, divide by 255 to undo the
// scaling before, store the new values and advance
// the pointer we're reading pixel data from
linePointer[0] = r * linePointer[3]/255;
linePointer[1] = g * linePointer[3]/255;
linePointer[2] = b * linePointer[3]/255;
}
linePointer += 4;
}
}
// get a CG image from the context, wrap that into a
// UIImage
CGImageRef cgImage = CGBitmapContextCreateImage(context);
UIImage *returnImage = [UIImage imageWithCGImage:cgImage];
// clean up
CGImageRelease(cgImage);
CGContextRelease(context);
free(memoryPool);
// and return
return returnImage;
}
我在UIImage扩展类中添加了上述方法。
3
首先,您必须将核心图像框架添加到您的项目中。
项目设置 - >目标 “项目名称” - >生成阶段 - >链接二进制与图书馆 - >添加项目 - > CoreImage.framework
其次,导入核心映像头到你的实现文件。
#import <CoreImage/CoreImage.h>
初始化一个UIImage
对象来存储原始文件。
UIImage *inputImage = [UIImage imageNamed:@"imageNamed"];
创建CIFilter
来定义您要如何修改原UIImage
对象。
CIFilter* filter = [CIFilter filterWithName:@"CIColorInvert"];
[filter setDefaults];
[filter setValue:inputImage.CIImage forKey:@"inputImage"];
创建另一个UIImage
对象以保留修改的图像。
UIImage *outputImage = [[UIImage alloc] initWithCIImage:filter.outputImage];
Voilà!希望它会有所帮助。
4
斯威夫特
使用CIContext
代替-UIImage:CIImage
(见https://stackoverflow.com/a/28386697/218152),和建筑在@wtznc's response,这里是一个自包含IBDesignable:
@IBDesignable
class InvertImage: UIImageView {
@IBInspectable var originalImage:UIImage? = nil
@IBInspectable var invert:Bool = false {
didSet {
var inverted = false
if let originalImage = self.originalImage {
if(invert) {
let image = CIImage(CGImage: originalImage.CGImage!)
if let filter = CIFilter(name: "CIColorInvert") {
filter.setDefaults()
filter.setValue(image, forKey: kCIInputImageKey)
let context = CIContext(options: nil)
let imageRef = context.createCGImage(filter.outputImage!, fromRect: image.extent)
self.image = UIImage(CGImage: imageRef)
inverted = true
}
}
}
if(!inverted) {
self.image = self.originalImage
}
}
}
}
要使用它,设置原始图像而不是图片自图片将动态关联:
0
Swift3
extension UIImage {
func invertedImage() -> UIImage? {
guard let cgImage = self.cgImage else { return nil }
let ciImage = CoreImage.CIImage(cgImage: cgImage)
guard let filter = CIFilter(name: "CIColorInvert") else { return nil }
filter.setDefaults()
filter.setValue(ciImage, forKey: kCIInputImageKey)
let context = CIContext(options: nil)
guard let outputImage = filter.outputImage else { return nil }
guard let outputImageCopy = context.createCGImage(outputImage, from: outputImage.extent) else { return nil }
return UIImage(cgImage: outputImageCopy)
}
}
0
对于Xamarin C#(IOS的特定;不是跨平台的解决方案):
public static UIImage InvertImageColors(UIImage original)
{
return ApplyFilter(original, new CIColorInvert());
}
public static UIImage ApplyFilter(UIImage original, CIFilter filter)
{
UIImage result;
try {
CIImage coreImage = original.CGImage;
filter.SetValueForKey(coreImage, CIFilterInputKey.Image);
CIImage output = filter.OutputImage;
CIContext context = CIContext.FromOptions(null);
CGImage cgImage = context.CreateCGImage(output, output.Extent);
result = UIImage.FromImage(cgImage);
} catch (Exception ex) {
// .. Log error here ..
result = original;
}
return result;
}
注#1:改编自别人的答案;我失去了原来的位置。
注意#2:每行只需一步,故意做到这一点,所以您可以看到中间类型,并轻松适应其他情况。
相关问题
- 1. 旋转UIImage的给予空白的UIImage
- 2. 从白色到黑色,工具或iPhone sdk的反转图标
- 3. 获取黑白UIImage(非灰度)
- 4. PDF转换为黑白PNG
- 5. 是否可以使用css反转黑白图像?
- 6. GLSL黑到白的过渡
- 7. iPhone SDK反转UIImage的颜色
- 8. 的NSString的NSData到UIImage的,反之亦然转换
- 9. 转换的UIImageView到的UIImage
- 10. 如何转换到UIImage的
- 11. 转换CMSampleBufferRef到的UIImage
- 12. 将UIImage转换为NSString(反之亦然)
- 13. 在OpenCV中将RGB转换为黑白
- 14. 将JPEG图像转换为黑白
- 15. 将灰度图像转换为黑白
- 16. C#转换图像来完成黑白
- 17. GDI System.Drawing.Printing将白色转换为黑色
- 18. bash命令反转文件路径的白名单以生成黑名单
- 19. 是否存在用于反转(黑底白字)排版的蓝图插件?
- 20. 转换UIScrollViewPoint到的UIImage + ZOOM +旋转
- 21. 白黑屏
- 22. SVG黑白
- 23. 更换白黑
- 24. 转换UIimage(拍照)到NSData
- 25. 从黑与白的过渡到颜色?
- 26. 转换mp4从彩色到黑白与linux终端
- 27. 直接转换黑白图像:3通道到1通道?
- 28. 是否有attr_readonly的白名单反转?
- 29. 从RGBA UIImage Alpha变白色
- 30. 转换的NSData到的UIImage - 用的NSURLRequest
您的意思是黑色背景和白色签名? – Shashi3456643 2015-02-07 21:41:53
@ Shashi3456643,是的,这就是我需要的 – Maystro 2015-02-07 21:43:02