2013-01-05 2 views
2

여기 블록과 ALAssetLibrary 루틴으로 조금 고생하고 있습니다. 나는 WWDC + Stanford 동영상을 블록을 통해 읽었으며 조금 읽었지만 아직 나를 위해 클릭하지는 못했습니다.ALAssetsGroupAlbum의 posterImage를 앨범 이름으로 가져 오기

내가하려는 것은 커스텀 포토 라이브러리를위한 posterImage를 얻는 것이다. 아래의 첫 번째 루틴은 내 mainViewController에 있으며 ALAssetLibrary 클래스 확장에있는 getPosterImageForAlbum: 함수를 호출합니다.

/시도에 대해 내가/혼란과 어려움을 겪고 오전 일 :

  1. 내가 참조 (CGImageRef 나있는 UIImage)에 전달하고 ALAssetLibrary 방법을 설정하거나에 대한 반환 값을 정의해야 하는가를 ALAssetLibrary 메서드를 사용하고 메인 클래스의 이미지를 처리합니까? 나는 두 가지 방법으로 성공하지 못했습니다.

  2. ALAssetLibrary 열거 형 메서드의 비동기 특성은 종류로 작동하기가 어려워서 잘못 처리 한 것 같습니다.

  3. param으로 전달할 블록 정의 : 항상 typedef가 필요합니까?

나는 모든 개념적 비트와 조각들이 소용돌이 치고 있다고 생각하지만 블록으로 작업하는 것을 명확하게 이해하지 못했습니다. 좋은 기사에 대한 조언이나 조언 *은 많은 도움이 될 것입니다. //

- (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; 
      } 
     } 
    }]; 
} 

// 내가 게시 된 후에는 난 정말 당신이 무엇을 이해 않았기 때문에 이것은 내가 큰 방법으로 내 원래의 대답을 편집있어 ALAssetLibrary

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; 
} 

답변

2

의 확장에 ALAssetLibrary에 클래스의 확장자를 작성하여 이름으로 앨범의 포스터 이미지를 찾는 대신 이름으로 앨범의 포스터 이미지를 찾습니다.

매개 변수로 UIImage 형식의 단일 매개 변수 (또는 선택적으로 NSError 매개 변수)를 받아들이는 완료 블록을 사용하는 확장 메서드를 작성하여이 문제를 처리 할 것입니다. 완료 블록에서 호출자는 비동기 적으로 반환 된 이미지로 원하는 모든 작업을 수행 할 수 있습니다. 블록을 형식 정의를 할 필요가 없습니다 - 당신이 방법을 이런 식으로 쓸 수있다 : 당신이 방법이 방법으로 부를 수있는, 그런

- (void) getPosterImageForAlbumNamed: (NSString *) albumName completionBlock: (void (^)(UIImage *, NSError *)) completionBlock 
{ 
    __block ALAssetsGroup * foundAlbum = nil; 
    [self enumerateGroupsWithTypes: ALAssetsGroupAlbum 
          usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 
           if (group) { 
            if ([(NSString *) [group valueForProperty: ALAssetsGroupPropertyName] compare: albumName] == NSOrderedSame) { 
             *stop = YES; 
             foundAlbum = group; 
             completionBlock([UIImage imageWithCGImage: [group posterImage]], nil); 
            } 
           } else{ 
            if (! foundAlbum) { 
             NSLog(@"Album wasn't found"); 
             // completionBlock(nil, XX SOME ERROR XX); 
            } 
           } 
          } failureBlock:^(NSError *error) { 
           NSLog(@"Couldn't access photo library"); 
           // completionBlock(nil, XX SOME ERROR XX); 
          }]; 

} 

을 :

-(IBAction) getPosterForAlbum: (id) sender 
{ 
    NSString * albumName = self.textField.text; 
    ALAssetsLibrary * library = [[ALAssetsLibrary alloc] init]; 

    __weak typeof(self) weakSelf = self; 
    [library getPosterImageForAlbumNamed: albumName completionBlock:^(UIImage * image, NSError * error) { 
     if (! error) { 
      [weakSelf doSomethingWithPosterImage: image]; 
     } 
    }]; 
} 

은 당신 무엇의 라인을 따라 할려고하는거야? (주요 편집 죄송 ...)

+0

시간을내어 주셔서 감사합니다! 나는 2 차 커피가 차고 난 후에이 코드를 씹어 야 할 것이다. 나는 코드 튜토리얼 코드로 시작했기 때문에 클래스 확장을 사용하고 있었다. (코드가 많은 길은 청소 된 코드로 포장되어있다.) –