在 SO 上四处寻找答案。发现了一些有趣的东西,但我仍然卡住了。
在视图控制器中,我有一个 UIImageView,我在视图加载时设置了从 UIImagePicker 获取的图像。(我也同时设置了一个CIImage)
我有两个滑块(一个用于亮度,一个用于对比度)。移动滑块后,对 CIImage 应用过滤器,然后将 CIImage 渲染到 UIImageView 的 UIImage 中。
拳头,从 UIImagePicker 获取的 UIImage 确实在第一次选择时正确显示。
我还正确设置了我的滑块范围,并验证了正确的浮点值正在传递给委托函数(通过 NSLog)。
但是,当我尝试使用滑块时,我的 UIImageView 变白了!也许你们可以帮忙。我将发布一些代码片段:
首先,我的@interface:
@interface PrepViewController : UIViewController <UIImagePickerControllerDelegate , UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *editingImage;
@property (weak, nonatomic) CIImage *editingCIImage;
@property (weak, nonatomic) CIContext *editingCIContext;
@property (weak, nonatomic) CIFilter *editingCIFilter;
@property (nonatomic) BOOL didAskForImage;
@property (weak, nonatomic) IBOutlet UISlider *brightnessSlider;
@property (weak, nonatomic) IBOutlet UISlider *contrastSlider;
- (void) doImageFilter:(NSString *)filter withValue:(float)value;
@end
我的 viewDidLoad,非常简单,只是将 didAskForImage 设置为 NO
- (void)viewDidLoad
{
[super viewDidLoad];
self.didAskForImage = NO;
}
viewDidAppear 是我检查我是否已经请求图像的地方,然后请求它
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
if(!self.didAskForImage){
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
sourceType = UIImagePickerControllerSourceTypeCamera;
}
picker.sourceType = sourceType;
[self presentModalViewController:picker animated:YES];
}
}
这是 imagePickerController didFinishPickingImage 委托方法,这是我实际设置 UIImageView 的图像属性并初始化 CIImage、CIContext 和 CIFilter 对象的地方。
我还设置了 didAskForImage 布尔值和滑块目标。
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage : (UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
self.didAskForImage = YES;
self.editingImage.image = image;
self.editingCIImage = [[CIImage alloc] initWithImage:image];
self.editingCIContext = [CIContext contextWithOptions:nil];
self.editingCIContext = [CIContext contextWithOptions:nil];
self.editingCIFilter = [CIFilter filterWithName:@"CIColorControls" keysAndValues:kCIInputImageKey, self.editingCIImage, nil];
[self.contrastSlider addTarget:self action:@selector(contrastMove:) forControlEvents:UIControlEventValueChanged];
[self.brightnessSlider addTarget:self action:@selector(brightnessMove:) forControlEvents:UIControlEventValueChanged];
[picker dismissModalViewControllerAnimated:YES];
}
和取消代表
-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
[picker dismissModalViewControllerAnimated:YES];
picker = nil;
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}
这是滑块的委托方法。同样,NSLogs 显示预期值。
- (void) contrastMove:(id)sender{
NSLog(@"%f", [(UISlider *)sender value]);
[self doImageFilter:@"inputContrast" withValue:[(UISlider *)sender value]];
}
- (void) brightnessMove:(id)sender{
NSLog(@"%f", [(UISlider *)sender value]);
[self doImageFilter:@"inputBrightness" withValue:[(UISlider *)sender value]];
}
最后,这是我创建的 doImageFilter 方法,它实际上执行过滤器并重新分配 UIImageView(这是它变白的地方)
- (void) doImageFilter:(NSString *)filter withValue:(float)value{
[self.editingCIFilter setValue:[NSNumber numberWithFloat:value] forKey:filter];
CIImage *output = [self.editingCIFilter outputImage];
CGImageRef cgi = [self.editingCIContext createCGImage:output fromRect:[output extent]];
self.editingCIImage = output;
self.editingImage.image = [UIImage imageWithCGImage:cgi];
}
所以我有一种预感,CIImage 甚至没有被正确初始化。但我不知道为什么。已经在这里待了几个小时了。
感谢你的帮助!
干杯