CGContext和视网膜显示

时间:2011-12-17 14:57:27

标签: iphone xcode

人, 这是我的代码:

- (void) renderPageAtIndex:(NSUInteger)index inContext:(CGContextRef)ctx {
    //background
    CGContextSetFillColorWithColor(ctx, [[UIColor colorWithRed:((float)238 / 255.0f) green:((float)233 / 255.0f) blue:((float)215 / 255.0f) alpha:1] CGColor]);
    CGContextFillRect(ctx, CGRectInset(leavesView.bounds, 0, 0));
    //text
    CGContextSetTextDrawingMode(ctx, kCGTextFill);
    CGContextSetTextMatrix(ctx, CGAffineTransformMake(1.0, 0.0, 0.0, 1.0, 0.0, 0.0));
    CGContextSetFillColorWithColor(ctx, [[UIColor blackColor] CGColor]);
    UIGraphicsPushContext(ctx);

    CGContextSaveGState(ctx);
    CGContextTranslateCTM(ctx, 0.1f, leavesView.bounds.size.height);
    CGContextScaleCTM(ctx, 1.0f, -1.0f);

    CGRect textRect = CGRectMake(5, 5, leavesView.bounds.size.width-10, leavesView.bounds.size.height-10);
    if (pages.count > 0 && pages.count-1 >= index) {
        [[pages objectAtIndex:index] drawInRect:textRect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
    }

    CGContextRestoreGState(ctx);

    UIGraphicsPopContext();
}

它在iPhone 2G,3G,3GS上运行完美,但在新机型上我遇到了问题。

在视网膜上绘制的文字不会像标准中那样使分辨率加倍。

你有什么想法吗?

3 个答案:

答案 0 :(得分:16)

尝试设置图层contentScale变量,如下所示:

layer.contentsScale = [UIScreen mainScreen].scale;

这应该使代码适应视网膜/非视网膜显示。

答案 1 :(得分:3)

看起来您正在使用git上的Leaves项目。我在我的项目中遇到了同样的问题并按如下方式解决了这个问题。

首先,您需要根据设备屏幕比例在LeavesCache.m中调整在imageForPageIndex中创建的原始Core Graphics上下文的大小和比例。确保也缩放CGContextClipToRect。

- (CGImageRef) imageForPageIndex:(NSUInteger)pageIndex {
    CGFloat scale = [[UIScreen mainScreen] scale];  // we need to size the graphics context according to the device scale
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, 
                                             pageSize.width*scale, 
                                             pageSize.height*scale, 
                                             8,                     /* bits per component*/
                                             pageSize.width*scale * 4,  /* bytes per row */
                                             colorSpace, 
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

    CGColorSpaceRelease(colorSpace);
    CGContextClipToRect(context, CGRectMake(0, 0, pageSize.width*scale, pageSize.height*scale));

    CGContextScaleCTM(context, scale, scale);

    [dataSource renderPageAtIndex:pageIndex inContext:context];

    CGImageRef image = CGBitmapContextCreateImage(context);
    CGContextRelease(context);

    [UIImage imageWithCGImage:image];
    CGImageRelease(image);

    return image;
}

接下来,在LeavesView.m中的setUpLayers中,您需要将设备屏幕比例应用于初始化的每个CALayer。由Brad Larson在similar question发布:

  

默认情况下,您的CALayer不会以更高的Retina显示屏分辨率渲染其Quartz内容。您可以使用以下代码启用此功能:

layer.contentsScale = [[UIScreen mainScreen] scale];

所以这里是LeavesView.m中setUpLayers的前几行,我将设备比例应用于topPage CALayer。您需要对setUpLayers中初始化的所有CALayers和CAGradientLayers执行此操作:

- (void) setUpLayers {
    // Set the backgound image of the leave view to the book page
    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"open_book_page_turn.png"]];

    self.clipsToBounds = YES;

    topPage = [[CALayer alloc] init];
    topPage.contentsScale = [[UIScreen mainScreen] scale];  // We need to apply the device display scale to each layer
    topPage.masksToBounds = YES;
    topPage.contentsGravity = kCAGravityLeft;
    topPage.backgroundColor = [[UIColor whiteColor] CGColor];
    topPage.backgroundColor = [background CGColor];

    ...

}

答案 2 :(得分:0)

Swift 3

func getImageFromContext() -> UIImage{

    UIGraphicsBeginImageContextWithOptions(self.frame.size, false, UIScreen.main.scale)
    self.drawHierarchy(in: self.bounds, afterScreenUpdates: true)
    self.layer.render(in: UIGraphicsGetCurrentContext()!)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return image!
}