iOS-使用按钮反转UIImage [英] iOS - Invert a UIImage with button

查看:121
本文介绍了iOS-使用按钮反转UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的应用程序,可以在相机胶卷上拍照或选择现有照片,然后在UIImageView中显示图像.我希望有一个按钮,可以按一下并反转拾取的图像,然后将其显示在相机胶卷中.我的问题是,如何反转UIImage的颜色,然后使用新的反转后的颜色显示在ImageView中?我看过其他与此相关的文章,但都没有解释如何实现代码.我对xcode还是很陌生,希望能收到所有反馈.

I have a simple application where I can take an photo or choose and existing photo on the camera roll and then display the image in a UIImageView. I want to have a button that you can press and invert the picked image and then display that in the camera roll. My question is, how do invert the colors of a UIImage and then use the new inverted one to display in the ImageView? I have seen other posts related to this but none explain how implement the code. I'm fairly new to xcode and would appreciate all feedback.

这是我用于视图控制器的.m文件:

Here is my .m file for the view controller:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize imageView;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)takePhoto {
    picker1 = [[UIImagePickerController alloc] init];
    picker1.delegate = self;
    [picker1 setSourceType:UIImagePickerControllerSourceTypeCamera];
    [self presentViewController:picker1 animated:YES completion:nil];

}

-(IBAction)ChooseExisting {

    picker2 = [[UIImagePickerController alloc] init];
    picker2.delegate = self;
    [picker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [self presentViewController:picker2 animated:YES completion:nil];

}

-(IBAction)invertImage {

}

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    imageFinal = [info objectForKey:UIImagePickerControllerOriginalImage];
    [imageView setImage:imageFinal];
    [self dismissViewControllerAnimated:YES completion:nil];

}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [self dismissViewControllerAnimated:YES completion:nil];
}

@end

推荐答案

我看到negetiveImage方法有很多代码.这是一个使用Core Image滤镜的简单示例.

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

将Core Image Framework包含到您的项目中 导入核心Image标头.

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

#import <CoreImage/CoreImage.h>

下面的代码应返回反向的UIImage

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];

outputImage将被反转

The outputImage will be inverted

这篇关于iOS-使用按钮反转UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆