문제

I am not sure whether I can improve image quality but following code displays very poor image quality in PDF. I know its standard code to generate images from view but is there anything I could do to specify image quality or improve it?

- (void)renderView:(UIView*)view {
    UIGraphicsBeginImageContext(view.frame.size);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewAsImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [viewAsImage drawInRect:rect];
}
도움이 되었습니까?

해결책

You probably need to create a graphics context with a scale of 2 (retina) instead of the default 1. To do so, use UIGraphicsBeginImageContextWithOptions(view.frame.size, YES, 0.0);. This will create an image context with an opaque target (you can set the second parameter to NO if you're rendering a transparent image) and with a scale factor of your device's main screen.

다른 팁

Double the size!

- (void)renderView:(UIView*)view {
    CGSize newSize = view.frame.size;
    newSize.width = newSize.width * [UIScreen mainScreen].nativeScale;
    newSize.height = newSize.height * [UIScreen mainScreen].nativeScale;

    UIGraphicsBeginImageContext(newSize);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewAsImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    [viewAsImage drawInRect:rect];
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top