1

我有一个简单的应用程序,我可以在其中拍照或选择相机胶卷上的现有照片,然后在 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
4

2 回答 2

9

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

将核心图像框架包含到您的项目中导入核心图像标头。

#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 将被反转

于 2013-06-09T04:09:33.623 回答
0

为了给你一个简单的答案,你可以反转UIImageViewwhich 有你的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.

希望这会有所帮助,我祝你在你的应用程序中一切顺利:)

于 2013-06-10T05:39:27.353 回答