资产库未更新

时间:2013-04-19 16:13:56

标签: ios objective-c alassetslibrary

我正在使用我正在构建的应用中的照片库功能,因此用户可以从应用内拍摄照片,并将其保存到自定义相册中,并附加特殊元数据以识别哪些照片属于该应用。< / p>

保存图片后,我尝试重新加载显示它找到的内容的表格,并且它从未找到最近添加的图片。只有在我离开视图后再将其重新启动才会找到最新的图片。

我使用this类别保存到自定义相册。以下是我的代码中的一些摘录,显示了保存/加载过程

保存

NSMutableDictionary *realMetaData = [[NSMutableDictionary alloc] init];
[realMetaData setObject:metaData forKey:(NSString*)kCGImagePropertyTIFFDictionary];
[library saveImage:imageToSave toAlbum:albumName metadata:realMetaData withCompletionBlock:^(NSError *error) {
    if ( error != nil )
    {
        NSLog(@"Error saving picture? %@", error);
    }
    [self loadAlbum];
}];

装载

-(void) loadAlbum
{
    [library groupNamed:albumName withCompletionBlock:^(NSError *error, ALAssetsGroup *foundGroup) {
        if ( error == nil )
        {
            self.album = foundGroup;
            [self loadAssets];
        }
        else
        {
            NSLog(@"Error loading album: %@", error);
        }
    }];

}

-(void) loadAssets
{
//  self.assets = [NSMutableArray arrayWithCapacity:album.numberOfAssets];
    for ( id key in [events allKeys] )
    {
        NSArray *event = [events objectForKey:key];
        if ( event != nil && event.count > 0 )
        {
            [events removeObjectForKey:key];
        }
    }
    [album enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
        if ( result != nil )
        {
            NSDictionary *metaData = result.defaultRepresentation.metadata;
//          NSLog(@"Retrieved image metadata: %@", metaData);
            NSDictionary *tiffData = [metaData objectForKey:(NSString*)kCGImagePropertyTIFFDictionary];
            NSString *encodedData = [tiffData objectForKey:(NSString*)kCGImagePropertyTIFFImageDescription];

            if ( encodedData != nil )
            {
                NSArray *parts = [encodedData componentsSeparatedByString:@"##"];
                if ( parts.count == 3 )
                {
                        NSLog(@"Found picture named %@", [parts objectAtIndex:2]);
                        NSString *eventName = [parts objectAtIndex:1];
                        NSArray *foundEvent = [events objectForKey:eventName];
                        if ( foundEvent != nil )
                        {
                            NSMutableArray *mutableEvent = [foundEvent mutableCopy];
                            [mutableEvent addObject:result];
                            [events setObject:mutableEvent forKey:eventName];
                        }
                        else
                        {
                            NSMutableArray *newEvent = [[NSMutableArray alloc] init];
                            [newEvent addObject:result];
                            [events setObject:newEvent forKey:eventName];
                        }
                    }
            }
        }
        else
        {
            [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
        }
    }];
}

所有内容都正确地解码了元数据,只是我刚刚添加到库中的一个图像没有显示出来。

0 个答案:

没有答案