iOS - Invert a UIImage with button

懵懂的女人 提交于 2019-12-06 14:52:19

I see the negetiveImage method has a lot of code. Here's a simple one using Core Image filters.

Include Core Image Framework to your project Import the core Image header.

#import <CoreImage/CoreImage.h>

The below code should return you the inverted UIImage

UIImage *inputImage = [UIImage imageNamed:@"imageName"];
CIFilter* filter = [CIFilter filterWithName:@"CIColorInvert"];
[filter setDefaults];
[filter setValue:inputImage.CIImage forKey:@"inputImage"];
UIImage *outputImage = [[UIImage alloc] initWithCIImage:filter.outputImage];

The outputImage will be inverted

To give you an easy answer, you can invert the UIImageView which has your UIImage.

To invert it, just use

[yourImageView setTransform:CGAffineTransformMakeScale(-1, 1)]; // flip horizontally
[yourImageView setTransform:CGAffineTransformMakeScale(1, -1)]; // flip vertically
[yourImageView setTransform:CGAffineTransformMakeScale(-1, -1)]; // flip horizontally and vertically

and to bring it back to normal, use

[yourImageView setTransform:CGAffineTransformIdentity]; // bring the view back to normal.

Hope this helps and I wish you all the best with you app :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!