2012-09-24 5 views
2

내가있는 UIView TTT_WantsToBeRazorSharpView라는 사용자 지정을 푼다 품질 아이폰 OS에게

를 렌더링함으로써 UIView의에서 PDF를 생성 할 수 있습니다에서 PDF를 생성합니다. 이보기는 아무것도하지 않습니다,하지만 지금보기 (IB에 한 번)을 UIViewController에 세 번 그려아이폰 OS는 : UIView의 텍스트가 흐릿

NSString*txtPleaseHelp = NSLocalizedString(@"Hello, am I blurry again?",@""); 
CGContextShowTextAtPoint(ctx, 10, 50, [txtPleaseHelp cStringUsingEncoding:NSMacOSRomanStringEncoding], [txtPleaseHelp length]); 

이러한 라인 코드에 두 번있는 텍스트를 그릴 (아래 이미지 비교) :

TTT_WantsToBeRazorSharpView *customViewSharp = [[TTT_WantsToBeRazorSharpView alloc] initWithFrame:CGRectMake(10,250, 300, 80)]; 
[self.view addSubview:customViewSharp]; 

TTT_WantsToBeRazorSharpView *customViewBlurryButIKnowWhy = [[TTT_WantsToBeRazorSharpView alloc] initWithFrame:CGRectMake(361.5, 251.5, 301.5, 80.5)]; 
[self.view addSubview:customViewBlurryButIKnowWhy]; 

첫 번째 코드로 그려지는보기는 razorsharp이고 두 번째 코드는 그려지지 않았지만 rect의 쉼표 값 (361.5, 251.5, 301.5, 80.5)으로 인해 괜찮습니다.

이 사진 참조 : See this picture

을하지만 내 문제는 내가 PDF 문서로보기를 렌더링하는 경우가 흐릿 지금입니다! blurry pdf

https://raw.github.com/florianbachmann/GeneratePDFfromUIViewButDontWantToLooseQuality/master/Test.pdf

있는 test.pdf PDF 파일 자체를하고 선은 PDF로보기를 렌더링 : 왜 모르는 여기를 참조

//this is blurry, but why? it can't be the comma coordinates 
CGRect customFrame1 = CGRectMake(10,50, 300, 80); 
TTT_WantsToBeRazorSharpView *customViewSharp = [[TTT_WantsToBeRazorSharpView alloc] initWithFrame:customFrame1]; 
CGContextSaveGState(context); 
CGContextSetTextMatrix(context, CGAffineTransformIdentity); 
CGContextTranslateCTM(context, (int)customFrame1.origin.x, (int)customFrame1.origin.y); 
[customViewSharp.layer renderInContext:context]; 
CGContextRestoreGState(context); 

그렇다면 renderInContext:context 텍스트가 흐릿한 이유는 무엇입니까? 나는 모든 힌트와 도움을 주셔서 감사합니다

은, 난 (소스 포함) 당신의 용기에 대한 GitHub의 프로젝트를 만든 : https://github.com/florianbachmann/GeneratePDFfromUIViewButDontWantToLooseQuality

답변

1

이 시도 :

- (void)initValues { 
    UIColor *gray = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.3f]; 
    CALayer *l = [self layer]; 
    l.masksToBounds = YES; 
    l.cornerRadius = 10.0f; 
    l.borderWidth = 3.0f; 
    self.backgroundColor = gray; 
    l.backgroundColor = gray.CGColor; 
    l.borderColor = gray.CGColor; 
} 

- (void)drawRect:(CGRect)rect { 
    NSLog(@"TTT_WantsToBeRazorSharpView drawRect[%3.2f,%3.2f][%3.2f,%3.2f]",rect.origin.x,rect.origin.y,rect.size.width,rect.size.height); 

    // get the graphic context 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextSetShouldSmoothFonts(ctx,YES); 

    CGColorRef colorWhite = CGColorRetain([UIColor redColor].CGColor); 

    CGContextSetFillColorWithColor(ctx, colorWhite); 

    CGContextSelectFont(ctx, "Helvetica-Bold", 24, kCGEncodingMacRoman); 
    CGAffineTransform tranformer = CGAffineTransformMakeScale(1.0, -1.0); 
    CGContextSetTextMatrix(ctx, tranformer); 

    //why is this sucker blurry? 
    NSString*txtPleaseHelp = NSLocalizedString(@"Hello, am I blurry again?",@""); 
    CGContextShowTextAtPoint(ctx, 10, 50, [txtPleaseHelp cStringUsingEncoding:NSMacOSRomanStringEncoding], [txtPleaseHelp length]); 
    CGColorRelease(colorWhite); 
} 

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { 

    if (!layer.shouldRasterize && !CGRectIsEmpty(UIGraphicsGetPDFContextBounds())) { 
     [self drawRect:self.bounds]; 
    } 
    else { 
     [super drawLayer:layer inContext:ctx]; 
    } 
} 

나는 그것을 볼 수 있도록 빨간색 텍스트 색상을 변경했다. 텍스트가 먼저 쓰여지기 때문에 투명 회색 버튼이 그 위에 놓여지면 흰색 텍스트가 사라집니다. drawLayer 메서드를 추가 할 때 래스터화할 필요가없는 모든 내용이 vector to pdf로 작성되므로 텍스트도 선택할 수 있습니다.

+0

와아! 대단히 감사합니다! 그 완벽한 솔루션입니다 :-) (지금 shouldRasterize 플래그 및 drawLayer 메서드를 알고 있어요 :-) – Flori

+0

이 코드를 UIViewController 사용할 수 있습니까? [self.view layer] .delegate = self; drawLayer : inContext :를 구현하는 것이 작동하지 않는 것 (그리고 충돌) – Flori

0

뷰의 텍스트가 PDF에서 비트 맵으로 렌더링이 아닌 벡터로한다 그래픽 및 이로 인해 흐린 모양. 내 생각 엔 iOS 렌더링 엔진이 중간 비트 맵에 텍스트를 그려서 (생각하는 색상 구성 목적으로)이 비트 맵이 대상 컨텍스트에서 렌더링됩니다.

self.frame = CGRectIntegral(self.frame); 
텍스트가 흐릿 이유는 정렬이 적합하지이었다이었다

는, 즉 분수 지점의 좌표 값 때문에 텍스트가 과도한로 흐릿하게 나타납니다

0

나는 텍스트가 흐릿를 해결하기 위해 다음 명령을 사용했다 안티 앨리어싱.

+0

아, 그 중 하나를 알고 좋은 :-) – Flori