cocoaTouch/HogHatViewController.m
changeset 3308 b6dcae4b6d2c
parent 3305 91074496d5c9
child 3312 6d8f1c76756d
equal deleted inserted replaced
3307:275af46392bd 3308:b6dcae4b6d2c
     8 
     8 
     9 #import "HogHatViewController.h"
     9 #import "HogHatViewController.h"
    10 
    10 
    11 
    11 
    12 @implementation HogHatViewController
    12 @implementation HogHatViewController
    13 @synthesize hatList, hog;
    13 @synthesize hatList, hatSprites, hog;
    14 
    14 
    15 #pragma mark -
    15 #pragma mark -
    16 #pragma mark View lifecycle
    16 #pragma mark View lifecycle
    17 
    17 
    18 
    18 
    22     //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    22     //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    23     NSString *hatPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/Data/Graphics/Hats/"];
    23     NSString *hatPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/Data/Graphics/Hats/"];
    24     NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatPath
    24     NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatPath
    25                                                                          error:NULL];
    25                                                                          error:NULL];
    26     self.hatList = array;
    26     self.hatList = array;
       
    27     NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:[hatList count]];
       
    28     for (int i=0; i< [hatList count]; i++) {
       
    29         NSString *hatFile = [hatPath stringByAppendingString:[hatList objectAtIndex:i]];
       
    30         
       
    31         UIImage *image = [[UIImage alloc] initWithContentsOfFile: hatFile];
       
    32         CGRect firstSpriteArea = CGRectMake(0, 0, 32, 32);
       
    33         CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], firstSpriteArea);
       
    34         [image release];
       
    35         
       
    36         UIImage *hatSprite = [[UIImage alloc] initWithCGImage:cgImgage];
       
    37         [spriteArray addObject:hatSprite];
       
    38         CGImageRelease(cgImgage);
       
    39         [hatSprite release];
       
    40     }
       
    41     self.hatSprites = spriteArray;
       
    42     [spriteArray release];
    27     //NSLog(@"%@", hatList);
    43     //NSLog(@"%@", hatList);
    28 }
    44 }
    29 
    45 
    30 - (void)viewWillAppear:(BOOL)animated {
    46 - (void)viewWillAppear:(BOOL)animated {
    31     [super viewWillAppear:animated];
    47     [super viewWillAppear:animated];
    32     self.title = [hog objectForKey:@"hogname"];
    48     self.title = [hog objectForKey:@"hogname"];
       
    49     // this updates the hog name and its hat
    33     [self.tableView reloadData];
    50     [self.tableView reloadData];
       
    51     // this moves the tableview to the top
       
    52     [self.tableView setContentOffset:CGPointMake(0, 0) animated:NO];
    34 }
    53 }
    35 
    54 
    36 /*
    55 /*
    37 - (void)viewDidAppear:(BOOL)animated {
    56 - (void)viewDidAppear:(BOOL)animated {
    38     [super viewDidAppear:animated];
    57     [super viewDidAppear:animated];
    90             cell.accessoryType = UITableViewCellAccessoryCheckmark;
   109             cell.accessoryType = UITableViewCellAccessoryCheckmark;
    91         } else {
   110         } else {
    92             cell.accessoryType = UITableViewCellAccessoryNone;
   111             cell.accessoryType = UITableViewCellAccessoryNone;
    93         }
   112         }
    94         
   113         
    95         NSString *hatsPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/Data/Graphics/Hats/"];
   114         cell.imageView.image = [hatSprites objectAtIndex:[indexPath row]];
    96         NSString *hatFile = [hatsPath stringByAppendingString:[hatList objectAtIndex:[indexPath row]]];
       
    97         UIImage *image = [UIImage imageWithContentsOfFile: hatFile];
       
    98 
       
    99         CGRect firstSpriteArea = CGRectMake(0, 0, 32, 32);
       
   100         CGImageRef cgImgage = CGImageCreateWithImageInRect([image CGImage], firstSpriteArea);
       
   101         cell.imageView.image = [UIImage imageWithCGImage: cgImgage];
       
   102         CGImageRelease(cgImgage);
       
   103     }
   115     }
   104     
   116     
   105     return cell;
   117     return cell;
   106 }
   118 }
   107 
   119