我的podspec中有一个提到资源的私有pod,包括故事板和pdf资源(矢量图像)
所有资源都按预期复制,我可以使用NSBundle(forClass:)
访问它们
有没有办法从使用此pod作为框架的项目创建UIImages?
在我的项目中,XCode在构建时从pdf生成@ 1,x2,x3 png,它们位于资产目录中并被称为"单个向量"。
但是如何实现框架呢?
答案 0 :(得分:3)
是!
-(void)splitPDF:(NSURL *)sourcePDFUrl withOutputName:(NSString *)outputBaseName intoDirectory:(NSString *)directory
{
CGPDFDocumentRef SourcePDFDocument = CGPDFDocumentCreateWithURL((__bridge CFURLRef)sourcePDFUrl);
size_t numberOfPages = CGPDFDocumentGetNumberOfPages(SourcePDFDocument);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathAndDirectory = [documentsDirectory stringByAppendingPathComponent:directory];
NSError *error;
if (![[NSFileManager defaultManager] createDirectoryAtPath:filePathAndDirectory
withIntermediateDirectories:NO
attributes:nil
error:&error])
{
NSLog(@”Create directory error: %@”, error);
return;
}
for(int currentPage = 1; currentPage <= numberOfPages; currentPage ++ )
{
CGPDFPageRef SourcePDFPage = CGPDFDocumentGetPage(SourcePDFDocument, currentPage);
// CoreGraphics: MUST retain the Page-Refernce manually
CGPDFPageRetain(SourcePDFPage);
NSString *relativeOutputFilePath = [NSString stringWithFormat:@"%@/%@%d.png", directory, outputBaseName, currentPage];
NSString *ImageFileName = [documentsDirectory stringByAppendingPathComponent:relativeOutputFilePath];
CGRect sourceRect = CGPDFPageGetBoxRect(SourcePDFPage, kCGPDFMediaBox);
UIGraphicsBeginPDFContextToFile(ImageFileName, sourceRect, nil);
UIGraphicsBeginImageContext(CGSizeMake(sourceRect.size.width,sourceRect.size.height));
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(currentContext, 0.0, sourceRect.size.height);
//596,842 //640×960,
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextDrawPDFPage (currentContext, SourcePDFPage);
// draws the page in the graphics context
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:relativeOutputFilePath];
[UIImagePNGRepresentation(image) writeToFile: imagePath atomically:YES];
}
}
您可以进一步查找更多here。