2

在这里用块和 ALAssetLibrary 例程有点挣扎。我已经浏览了 WWDC + Stanford 视频块并阅读了一些内容,但对我来说还没有完全点击。

我想要做的是获取自定义照片库的海报图像。下面的第一个例程在我的主 viewController 中,并调用getPosterImageForAlbum:ALAssetLibrary 类扩展中的函数。

我正在努力/困惑/尝试过的事情:

  1. 我应该传入一个引用(CGImageRef 或 UIImage)并让 ALAssetLibrary 方法设置它还是为 ALAssetLibrary 方法定义一个返回值,然后在我的主类中处理图像?我已经尝试了两种方式都没有成功。

  2. ALAssetLibrary 枚举方法的异步性质有点难以使用 - 所以我想我做错了。

  3. 定义要作为参数传递的块:我总是需要 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;
}
4

1 回答 1

2

我正在大量编辑我的原始答案,因为只有在我发布它之后,我才真正理解你想要做什么——在 ALAssetLibrary 上编写一个类扩展,它可以按名称查找专辑的海报图片,而不仅仅是查找专辑的以任何可能的方式按名称命名海报图像。

我将通过编写一个扩展方法来处理这个问题,该方法将一个完成块作为参数,该完成块接受一个 UIImage 类型的参数(或可选的 NSError 参数)。在该完成块中,调用者可以对异步返回的图像做任何他们想做的事情。不需要 typedef 块——你可以这样写方法:

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

这是否与您正在尝试做的事情一致?(对不起,主要编辑......)

于 2013-01-05T05:23:59.617 回答