[from now] 2012/09/07 01:02:51

diff --git a/ImageFilterChecker/Classes/Controllers/FilterViewController.h b/ImageFilterChecker/Classes/Controllers/FilterViewController.h
index a3a4556..beb5b8e 100644
--- a/ImageFilterChecker/Classes/Controllers/FilterViewController.h
+++ b/ImageFilterChecker/Classes/Controllers/FilterViewController.h
@@ -5,15 +5,31 @@
 //  Last Change: 2012-09-07.
 //

+@protocol FilterViewControllerDelegate;
+
 @interface FilterViewController : UIViewController

-@property (weak) UIImageView* imageView;
+@property (weak) id<FilterViewControllerDelegate> delegate;
+@property (weak) UIImage* targetImage;
 @property (copy) NSString* filterName;

 - (void)setParameterWithDictionary:(NSDictionary*)dic;

 @end

+#pragma mark - FilterViewControllerDelegate
+
+@protocol FilterViewControllerDelegate
+@required
+
+- (void)filterViewController:(FilterViewController*)filterViewController
+                 applyFilter:(NSString*)filterName
+                  withValues:(NSArray*)values;
+
+- (void)filterViewController:(FilterViewController*)filterViewController updateImage:(UIImage*)image;
+
+@end
+
 #pragma mark - Protected

 @interface FilterViewController (Protected)
diff --git a/ImageFilterChecker/Classes/Controllers/FilterViewController.m b/ImageFilterChecker/Classes/Controllers/FilterViewController.m
index a59067e..7453d87 100644
--- a/ImageFilterChecker/Classes/Controllers/FilterViewController.m
+++ b/ImageFilterChecker/Classes/Controllers/FilterViewController.m
@@ -15,7 +15,6 @@
 @property (weak) IBOutlet UITableView* tableView;
 @property (strong) NSArray* parameters;
 @property (strong) NSMutableDictionary* values;
-@property (strong) UIImage* defaultImage;

 @end

@@ -36,17 +35,20 @@
   return self;
 }

-- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
+- (void)loadView
 {
-  return ( UIInterfaceOrientationPortrait == interfaceOrientation );
+  [super loadView];
+
+  UIBarButtonItem* applyButton = [[UIBarButtonItem alloc] initWithTitle:@"Apply"
+                                                                  style:UIBarButtonItemStyleBordered
+                                                                 target:self
+                                                                 action:@selector(applyButtonDidPush)];
+  self.navigationItem.rightBarButtonItem = applyButton;
 }

-- (void)viewDidDisappear:(BOOL)animated
+- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
-  [super viewDidDisappear:animated];
-  if (self.defaultImage) {
-    self.imageView.image = self.defaultImage;
-  }
+  return ( UIInterfaceOrientationPortrait == interfaceOrientation );
 }

 #pragma mark - UITableViewDataSource
@@ -75,10 +77,7 @@

 - (void)executeFiltering
 {
-  if (nil == self.defaultImage) {
-    self.defaultImage = self.imageView.image;
-  }
-  CIImage* ciImage = [CIImage imageWithCGImage:self.defaultImage.CGImage];
+  CIImage* ciImage = [CIImage imageWithCGImage:self.targetImage.CGImage];
   CIFilter* filter = [self filterForCIImage:ciImage];
   for (id key in self.values) {
     [filter setValue:[self.values objectForKey:key] forKey:key];
@@ -87,7 +86,7 @@
   CIContext* context = [CIContext contextWithOptions:nil];
   CGImageRef imageRef = [context createCGImage:ciOutputImage fromRect:[ciOutputImage extent]];
   UIImage* outputImage = [UIImage imageWithCGImage:imageRef];
-  self.imageView.image = outputImage;
+  [self.delegate filterViewController:self updateImage:outputImage];
 }

 #pragma mark - ParameterCellDelegate
@@ -112,4 +111,14 @@
   return filter;
 }

+#pragma mark - Private Methods
+
+- (void)applyButtonDidPush
+{
+  [self.delegate filterViewController:self
+                          applyFilter:self.filterName
+                           withValues:[self.values copy]];
+  [self.navigationController popViewControllerAnimated:YES];
+}
+
 @end
diff --git a/ImageFilterChecker/Classes/Controllers/FiltersViewController.m b/ImageFilterChecker/Classes/Controllers/FiltersViewController.m
index 51e34f9..2bfb490 100644
--- a/ImageFilterChecker/Classes/Controllers/FiltersViewController.m
+++ b/ImageFilterChecker/Classes/Controllers/FiltersViewController.m
@@ -10,9 +10,12 @@

 #pragma mark - Private Definition ------------------------------

-@interface FiltersViewController ()
+@interface FiltersViewController () <FilterViewControllerDelegate>
 @property (weak) IBOutlet UITableView* tableView;
 @property (strong) NSArray* filters;
+@property (strong) UIImage* defaultImage;
+@property (strong) UIImage* currentImage;
+@property (strong) NSMutableArray* appliedFilters;
 @end

 #pragma mark - Main Implementation -----------------------------
@@ -23,8 +26,7 @@

 - (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
 {
-  if ( self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil] ) {
-    // Custom initialization
+  if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
   }
   return self;
 }
@@ -38,6 +40,11 @@
 {
   [super viewDidLoad];

+  self.appliedFilters = [NSMutableArray array];
+
+  self.defaultImage = self.imageView.image;
+  [self updateImage:self.defaultImage];
+
   NSString* fileName = [[NSBundle mainBundle] pathForResource:@"filters" ofType:@"plist"];
   NSDictionary* dic = [NSDictionary dictionaryWithContentsOfFile:fileName];
   self.filters = [dic objectForKey:@"filters"];
@@ -47,6 +54,7 @@
 {
   [super viewWillAppear:animated];
   [self.tableView reloadData];
+  [self updateImage:self.currentImage];
 }

@@ -78,11 +86,43 @@
   NSString* className = [filter objectForKey:@"viewControllerName"];
   Class viewControllerClass = NSClassFromString(className);
   FilterViewController* viewController = [viewControllerClass new];
+  viewController.delegate = self;
   viewController.title = filterName;
   viewController.filterName = filterName;
-  viewController.imageView = self.imageView;
+  viewController.targetImage = self.imageView.image;
   [viewController setParameterWithDictionary:filter];
   [self.navigationController pushViewController:viewController animated:YES];
 }

+#pragma mark - FilterViewControllerDelegate
+
+- (void)filterViewController:(FilterViewController*)filterViewController
+                 applyFilter:(NSString*)filterName
+                  withValues:(NSArray*)values
+{
+  [self updateImage:self.imageView.image];
+  [self.appliedFilters addObject:@{
+    @"filterName": filterName,
+    @"parameters": values
+  }];
+  NSLog( @"appliedFilters: %@", self.appliedFilters );
+}
+
+- (void)filterViewController:(FilterViewController*)filterViewController updateImage:(UIImage*)image
+{
+  self.imageView.image = image;
+}
+
+#pragma mark - Private Methods
+
+- (void)updateImage:(UIImage*)image
+{
+  if (image != self.currentImage) {
+    self.currentImage = image;
+  }
+  if (image != self.imageView.image) {
+    self.imageView.image = image;
+  }
+}
+
 @end
Add some actions