iOS - 使用按钮反转UIImage

时间:2013-06-08 23:09:11

标签: iphone ios xcode uiimageview uiimage

我有一个简单的应用程序,我可以在相机胶卷上拍照或选择现有照片,然后在UIImageView中显示图像。我想要一个按钮,您可以按下并反转拾取的图像,然后在相机胶卷中显示。我的问题是,如何反转UIImage的颜色,然后使用新的反转颜色在ImageView中显示?我已经看到了与此相关的其他帖子但没有解释如何实现代码。我是xcode的新手,非常感谢所有反馈。

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

#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

2 个答案:

答案 0 :(得分:9)

我看到negetiveImage方法有很多代码。这是一个使用Core Image过滤器的简单过滤器。

将Core Image Framework包含在您的项目中 导入核心图像标题。

#import <CoreImage/CoreImage.h>

以下代码应该返回倒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

答案 1 :(得分:0)

为了给您一个简单的答案,您可以将UIImageView转换为UIImage

要反转它,只需使用

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

并将其恢复正常,请使用

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

希望这会有所帮助,我希望你最好的应用程序:)