title: getting the posterImage of ALAssetsGroupAlbum by name of album
body: <p>Struggling a bit here with blocks and the ALAssetLibrary routines. I've been through the WWDC + Stanford videos on blocks and read up a bit but it hasn't quite clicked for me yet.</p>
  
  <p>What I am trying to do is get the posterImage for a custom photo library. The first routine below is in my main viewController and calls the function <code>getPosterImageForAlbum:</code> which is in an ALAssetLibrary class extension.</p>
  
  <p>The things I am struggling with/confused about/have tried:</p>
  
  <ol>
  <li><p>Should I pass in a reference (CGImageRef or UIImage) and have the
  ALAssetLibrary method set it or define a return value for the
  ALAssetLibrary method and then handle the image in my main class?
  I've tried it both ways without success.</p></li>
  <li><p>The asynchronous nature of the ALAssetLibrary enumeration methods are
  kind of hard to work with - so I guess I am doing it wrong. </p></li>
  <li><p>Defining a block to be passed as a param: do I always need to
  typedef it?</p></li>
  </ol>
  
  <p>I think I've got all the conceptual bits and pieces swirling around but I haven't yet been able to wrangle them into a clear understanding of working with blocks. Any tips or pointers* to good articles would be much appreciated.
  //</p>
  
  <pre><code>- (IBAction)getPosterImage:(id)sender {
  
      NSString *groupName = self.groupNameField.text;
      NSLog(@"%@", groupName);
  
      __weak typeof(self) weakSelf = self;
  
     CGImageRef tmpImg = [weakSelf.library getPosterImageForAlbum:groupName withCompletionBlock:(CGImageRef)(^GetPosterImageCompletion)(NSError *error){
          if (error!=nil) {
              NSLog(@"getPosterImage error: %@", [error description]);
          } else {
              if (tmpImg != nil){
                  UIImage * posterImg = [UIImage imageWithCGImage:tmpImg];
                  weakSelf.pImage.image = posterImg;
              }
          }
      }];
  }
  </code></pre>
  
  <p>// This is in an extension of the ALAssetLibrary</p>
  
  <pre><code>typedef CGImageRef(^GetPosterImageCompletion)(NSError* error);
  
  -(CGImageRef)getPosterImageForAlbum:(NSString*)albumName
                  withCompletionBlock:(GetPosterImageCompletion)completionBlock
  {
      __block BOOL albumWasFound = NO;
      __block CGImageRef thePosterImage = nil;
  
      SaveImageCompletion test;
      //search all photo albums in the library
      [self enumerateGroupsWithTypes:ALAssetsGroupAlbum
                          usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
  
              NSLog(@"this group name: %@",
                    [group valueForProperty:ALAssetsGroupPropertyName]);
  
              //compare the names of the albums              
              if ([albumName compare:
                   [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {
  
                  printf("matches \n");    //target album is found
                  albumWasFound = YES;
                  thePosterImage = group.posterImage;
                  *stop = true;
                  return ;
              }
  
          } failureBlock: test];
  
      if (albumWasFound==NO) {
          NSLog(@"%@", @"No group found");
      }
      return thePosterImage;
  }
  </code></pre>