CGContext和Retina显示屏

6

大家好,这是我的代码:

- (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上完美运行,但在新型号上我遇到了问题。

在视网膜屏幕上,文本绘制的分辨率不是像标准屏幕那样加倍。

你有什么想法吗?

4个回答

16

尝试像这样设置图层的contentScale变量:

layer.contentsScale = [UIScreen mainScreen].scale;

这样做可以使代码适应Retina/非Retina显示屏。


我已经对CALayer进行了子类化,并且我不得不在init方法中完成这个操作。在drawWithContext方法中,它会抛出一个只读错误。 - Chase Roberts

3

看起来你正在使用git上的Leaves项目。我在我的项目中遇到了同样的问题,并通过以下方式解决了它。

首先,你需要根据设备的屏幕比例调整在LeavesCache.m中创建的原始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在类似的问题中发布的内容:

默认情况下,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];

    ...

}

topPage.contentsScale = [[UIScreen mainScreen] scale]; 没有起到作用,看起来仍然像素化了... - Yaroslav Dukal

2

您可以在启动上下文时设置比例

+ (UIImage *)imageWithView:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions([view bounds].size, NO, [[UIScreen mainScreen] scale]);

    [[view layer] renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return result;
}

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!
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接