다음 방법을 사용하여 UIView에서 PDF를 생성했습니다. 그들 모두는 PDF를 만들 수 있지만, 품질 손실 :iOS - 렌더링으로 UIView에서 PDF를 생성하면 품질이 떨어집니다.
방법 1 :
@implementation UIView(PDFWritingAdditions)
- (void)renderInPDFFile:(NSString*)path
{
CGRect mediaBox = self.bounds;
CGContextRef ctx = CGPDFContextCreateWithURL((__bridge_retained CFURLRef)[NSURL fileURLWithPath:path], &mediaBox, NULL);
CGPDFPageRef page;
CGContextDrawPDFPage(ctx, page);
CGPDFContextBeginPage(ctx, NULL);
// Also tried following commented lines but no luck
// CGContextSetFlatness(ctx, 0.1);
// CGContextSetAllowsAntialiasing(ctx, YES);
// CGContextSetAllowsFontSmoothing(ctx, YES);
// CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -mediaBox.size.height);
[self.layer renderInContext:ctx];
CGPDFContextEndPage(ctx);
CFRelease(ctx);
}
@end
방법 2 :
- (void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];
// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[aView.layer renderInContext:pdfContext];
// remove PDF rendering context
UIGraphicsEndPDFContext();
// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];
// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
}
나는 확실하지 PDF로 컨텍스트에 대한 몇 가지 설정을 누락 될 수 있습니다 생각합니다. 또한 UIView에서 정확히 동일한 품질의 다음과 같은 방법으로 png 이미지를 만들었습니다.
- (UIImage *)imageFromView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:@"pdf.png"];
// instructs the mutable data object to write its context to a file on disk
[UIImagePNGRepresentation(img) writeToFile:documentDirectoryFilename atomically:YES];
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
return img;
}
내가 잘못하고있는 것을 누군가 볼 수 있습니까? 감사합니다. .
품질이 어떻게 떨어 집니까? –
원본보기 링크는 다음과 같습니다. 및 이후 pdf 링크 : –
iEamin
가능한 복제본 [고품질 PDF에서 UIImage] (http://stackoverflow.com/questions/14152477/high-quality-uiimage-from-pdf) – nielsbot