2012年1月29日 星期日

Filter4Cam 學習之 Core Image Filter With CATransition

since: 2012/01/29
update: 2012/01/29


A. 說明  
      這篇文章主要是參考之前的二篇文章: Filter4Cam 學習之 PocketCoreImage
      與 Filter4Cam 學習之 ViewTransitions 的內容. 延續 PocketCoreImage 的專案,
      利用 Core Animation transitions 來達成濾鏡套用時的特效切換. 目前只適用在
      靜態的圖片上, 動態的即時影片尚未測試.

---------------------------------------------------------------------------------------------

B. 開啓 PocketCoreImage 專案中的 ViewController.h 檔案, 修改如下:
#import <UIKit/UIKit.h>
//@add: 確認有 import QuartzCore/QuartzCore.h 進來
#import <QuartzCore/QuartzCore.h>
#import "FilteredImageView.h"

//@update
@interface ViewController : UIViewController <UINavigationControllerDelegate>
{
    //@add
    UITableView *tableView;
   
    // Array of CIFilters currently applied to the image.
    NSMutableArray *_filtersToApply;
   
    // Array created at startup containg the names of filters that can be applied to the image.
    NSArray *_availableFilters;
   
    //@add for CATransition
    BOOL transitioning;
}
....

---------------------------------------------------------------------------------------------

C. 開啓 ViewController.m 檔案, 修改如下:
    說明: 以下三種情況會作特效切換.
              1. 按下 "Clear All", 清除所有的濾鏡效果.
              2. 選取一個濾鏡來套用. 
              3. 取消已選取的濾鏡.
....
//@add for CATransition
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    transitioning = NO;
}
....
// 按下 "Clear All", 清除所有的濾鏡效果.
- (IBAction)clearFilters:(id)sender
{
    //@add for CATransition
    if(!transitioning)
    {
        CATransition *transition = [CATransition animation];
        transition.duration = 1.20;

        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

        transition.type = @"suckEffect";
        transitioning = YES;
        transition.delegate = self;
        [self.imageView.layer addAnimation:transition forKey:nil];
    }

    [_filtersToApply removeAllObjects];
   
    // Instruct the filtered image view to refresh
    [_imageView reloadData];
   
    // Instruct the table to refresh.  This will remove
    // any checkmarks next to selected filters.
    [_tableView reloadData];
}
....
// 選取一個濾鏡來套用. 
- (void)addFilter:(NSString*)name
{
    //@add for CATransition
    if(!transitioning)
    {
        CATransition *transition = [CATransition animation];
        transition.duration = 1.20;

        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

        transition.type = @"suckEffect";
        transitioning = YES;
        transition.delegate = self;
        [self.imageView.layer addAnimation:transition forKey:nil];
    }   
   
    // Create a new filter with the given name.
    CIFilter *newFilter = [CIFilter filterWithName:name];
    // A nil value implies the filter is not available.
    if (!newFilter) return;
   
    // -setDefaults instructs the filter to configure its parameters
    // with their specified default values.
    [newFilter setDefaults];
    // Our filter configuration method will attempt to configure the
    // filter with random values.
    [ViewController configureFilter:newFilter];
   
    [_filtersToApply addObject:newFilter];
   
    // Instruct the filtered image view to refresh
    [_imageView reloadData];
}
....
// 取消已選取的濾鏡.
- (void)removeFilter:(NSString*)name
{
    //@add for CATransition
    if(!transitioning)
    {
        CATransition *transition = [CATransition animation];
        transition.duration = 1.20;

        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

        transition.type = @"suckEffect";
        transitioning = YES;
        transition.delegate = self;
        [self.imageView.layer addAnimation:transition forKey:nil];
    }   
      
    NSUInteger filterIndex = NSNotFound;
   
    // Find the index named filter in the array.
    for (CIFilter *filter in _filtersToApply)
        if ([filter.name isEqualToString:name])
            filterIndex = [_filtersToApply indexOfObject:filter];
   
    // If it was found (which it always should be) remove it.
    if (filterIndex != NSNotFound)
        [_filtersToApply removeObjectAtIndex:filterIndex];
   
    // Instruct the filtered image view to refresh
    [_imageView reloadData];
}

---------------------------------------------------------------------------------------------

D. 編譯並執行:


沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。