修改main.m文件内代码
1.在@interface CUICatalog : NSObject下添加
-(id)initWithURL:(NSURL *)URL error:(NSError **)error;方法
2.修改
void exportCarFileAtPath(NSString * carPath, NSString *outputDirectoryPath) 方法:
void exportCarFileAtPath(NSString * carPath, NSString *outputDirectoryPath)
{
NSError *error = nil;
outputDirectoryPath = [outputDirectoryPath stringByExpandingTildeInPath];
//之前代码
// CUIThemeFacet *facet = [CUIThemeFacet themeWithContentsOfURL:[NSURL fileURLWithPath:carPath] error:&error];
//
// CUICatalog catalog = [[CUICatalog alloc] init];
//
// / Override CUICatalog to point to a file rather than a bundle */
// [catalog setValue:facet forKey:@"_storageRef"];
// 替换成以下代码
CUICatalog catalog = nil;
if ([CUICatalog instancesRespondToSelector:@selector(initWithURL:error:)]) {
/ If CUICatalog has the URL API (Mojave), use it. */
catalog = [[CUICatalog alloc] initWithURL:[NSURL fileURLWithPath:carPath] error:&error];
} else {
CUIThemeFacet facet = [CUIThemeFacet themeWithContentsOfURL:[NSURL fileURLWithPath:carPath] error:&error];
catalog = [[CUICatalog alloc] init];
/ Override CUICatalog to point to a file rather than a bundle /
[catalog setValue:facet forKey:@"_storageRef"];
}
NSCAssert(!error, @"Error attempting to open asset catalog (%@): %@", carPath, error);
/ CUICommonAssetStorage won't link */
CUICommonAssetStorage *storage = [[NSClassFromString(@"CUICommonAssetStorage") alloc] initWithPath:carPath];
for (NSString *key in [storage allRenditionNames])
{
printf("%s\n", [key UTF8String]);
NSArray* pathComponents = [key pathComponents];
if (pathComponents.count > 1)
{
// Create subdirectories for namespaced assets (those with names like "some/namespace/image-name")
NSArray* subdirectoryComponents = [pathComponents subarrayWithRange:NSMakeRange(0, pathComponents.count - 1)];
NSString* subdirectoryPath = [outputDirectoryPath copy];
for (NSString* pathComponent in subdirectoryComponents)
{
subdirectoryPath = [subdirectoryPath stringByAppendingPathComponent:pathComponent];
}
[[NSFileManager defaultManager] createDirectoryAtPath:subdirectoryPath
withIntermediateDirectories:YES
attributes:nil
error:&error];
}
NSMutableArray *images = getImagesArray(catalog, key);
for( CUINamedImage *image in images )
{
if( CGSizeEqualToSize(image.size, CGSizeZero) )
printf("\tnil image?\n");
else
{
CGImageRef cgImage = [image image];
NSString *idiomSuffix = idiomSuffixForCoreThemeIdiom(image.idiom);
NSString *sizeClassSuffix = @"";
if (image.sizeClassHorizontal || image.sizeClassVertical)
{
sizeClassSuffix = [NSString stringWithFormat:@"-%@x%@", sizeClassSuffixForSizeClass(image.sizeClassHorizontal), sizeClassSuffixForSizeClass(image.sizeClassVertical)];
}
NSString *scale = image.scale > 1.0 ? [NSString stringWithFormat:@"@%dx", (int)floor(image.scale)] : @"";
NSString *name = [NSString stringWithFormat:@"%@%@%@%@.png", key, idiomSuffix, sizeClassSuffix, scale];
printf("\t%s\n", [name UTF8String]);
if( outputDirectoryPath )
CGImageWriteToFile(cgImage, [outputDirectoryPath stringByAppendingPathComponent:name]);
}
}
}
}