2012年3月27日 星期二

Filter4Cam 實作: 14. 濾鏡表單之七

since: 2012/03/27
update: 2012/03/27

reference: I touchs: Filter4Cam 學習之 Scroll Horizontally Tables: Part 2

修正與效能調整之二

A. 選取 / 取消選取 濾鏡(Cell)

     1. 開啓  HorizontalTableCell.h 檔案, 修改如下:
....
@interface HorizontalTableCell : UITableViewCell <UITableViewDelegate, UITableViewDataSource>
{
    //@add
    // we will rotate and add as a subview of our cell
    UITableView *filterTableView;
   
    // hold the filters for the category we are in
    NSArray *filters;
   
    //@add: lastIndexPath in tableView:didSelectRowAtIndexPath:
    NSIndexPath *lastIndexPath;
}

//@add
@property (nonatomic, strong) UITableView *filterTableView;
@property (nonatomic, strong) NSArray *filters;
@property (nonatomic, strong) NSIndexPath *lastIndexPath;

@end

     2. 開啓  HorizontalTableCell.m 檔案, 修改如下:
....
//@add
@synthesize filterTableView = _filterTableView;
@synthesize filters = _filters;
@synthesize lastIndexPath = _lastIndexPath;
....
//@add: 選取 / 取消選取 filter cell 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    int newRow = [indexPath row];
    int oldRow = [self.lastIndexPath row];
   
    // 選取濾鏡
    if (newRow != oldRow)
    {
        self.lastIndexPath = indexPath;
       
        //@TODO: 套用濾鏡功能
       
    }
    // 取消選取濾鏡
    else
    {
        //self.lastIndexPath = nil; // -> not work at section:0 row:0
        self.lastIndexPath = [NSIndexPath indexPathForRow:-1 inSection:0];
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
       
        //@TODO: 恢復原始影像
       
    }
}
....

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

B. 效能調整: GCD (Grand Central Dispatch)
     1. 開啓 HorizontalTableCell.m 檔案, 修改如下:
....
//@add
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // we used the same reuse identifier as we did in the FilterCell class.
    static NSString *cellIdentifier = @"FilterCell";
   
    // we create a standard UITableViewCell for now
    //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    //
    //@update
    // dequeueReusableCellWithIdentifier: method will returns a regular UITableViewCell,
    // so we must cast it to our custom subclass(FilterCell).    
    //FilterCell *cell = (FilterCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
   
    //@update for GCD
    //
    // We added __block prior to our FilterCell cell variable,
    // that's going to allow us to use the cell variable inside blocks.
    __block FilterCell *cell = (FilterCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
   
    // If we don't get returned a reusable cell, 
    //
    // then we allocate and initialize one with a custom frame that positions it
    // at the very top left corner of its container and makes it
    // the same width and height as our cell.
    if (cell == nil)
    {
        //cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        //@update
        cell = [[FilterCell alloc] initWithFrame:CGRectMake(0, 0, kCellWidth, kCellHeight)];
    }
   
    //cell.textLabel.text = @"The title of the cell in the table within the table :O";
    //@update
    // we just fetch the current filter dictionary from our filters array
    //NSDictionary *currentFilter = [self.filters objectAtIndex:indexPath.row];
    //@update for GCD
    __block NSDictionary *currentFilter = [self.filters objectAtIndex:indexPath.row];
   
    //@add for GCD
    //
    // We create a new dispatch queue variable and as GCD
    // to give us one of the available queues.

    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
   
    //cell.sampleImage.image = [UIImage imageNamed:[currentFilter objectForKey:@"ImageName"]];
    //@update for GCD
    //
    dispatch_async(concurrentQueue, ^{  
       
        // Run the UIImage creation in the background
        UIImage *image = nil;       
        image = [UIImage imageNamed:[currentFilter objectForKey:@"ImageName"]];
       
        // Get the main thread, and set the sampleImage image to the one we just loaded asynchronously.
        dispatch_async(dispatch_get_main_queue(), ^{
            [cell.sampleImage setImage:image];
        });
    });
   
    //@title text: we don't need to load that, just read it from our array of filters. 
    cell.titleLabel.text = [currentFilter objectForKey:@"Title"];
   
    return cell;
}
....

     2. 編譯並執行
         當圖檔較多時, 你便會注意到圖片的載入較為順暢.

沒有留言:

張貼留言

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