在SO上搜索答案。發現了一些有趣的東西,但我仍然卡住了。核心圖像幫助:通過UIImagePicker從UIImage設置CIImage(然後過濾它)
在視圖控制器中,我有一個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是我檢查我我已經要求的圖像,然後要求它
這是imagePickerController didFin ishPickingImage委託方法 這是我實際設置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甚至沒有被正確初始化;但我不知道爲什麼。已經在這裏幾個小時了。
感謝您的幫助!
乾杯