With the code below, the CGContextDrawImage() painted layer has lower quality than the original displayed layer. Some lines in the image aliased.
Here is my code
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
CGContextSaveGState(ctx);
UIGraphicsBeginImageContext(layer.bounds.size);
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetShouldAntialias(c, YES);
CGContextSetAllowsAntialiasing(c, YES);
CGContextSetInterpolationQuality(c, kCGInterpolationHigh);
[self.view.layer renderInContext:c];
CGImageRef image = CGBitmapContextCreateImage(c);
UIGraphicsEndImageContext();
CGImageRef flipImage = CGImageCreateWithImageInRect(image, layer.bounds);
CGContextTranslateCTM(ctx, 0, layer.bounds.size.height);
CGContextScaleCTM(ctx, 1.0f, -1.0f);
CGContextSetShouldAntialias(ctx, YES);
CGContextSetAllowsAntialiasing(ctx, YES);
CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
CGContextDrawImage(ctx, layer.bounds, flipImage);
CGContextRestoreGState(ctx);
LogMessage(@"drawLayer", 1, @"draw layer content");
}
Update 1
The layer painted in drawLayer:
has different size from the screen. I want to slice the screen into pieces then apply animation to them.
original layer :
drawed layer :
Why do you open an image context in the first place? Draw directly onto the layers ctx
context passed as the second method argument.
Further you need to set the contentsScale
property of the layer to [UIScreen mainScreen].scale
.
To draw an image with the correct device scale (not needed in your example) use this:
void UIGraphicsBeginImageContextWithOptions(
CGSize size,
BOOL opaque,
CGFloat scale // pass 0 to use screen scale
);
PS: As of right now, I really can't see what you want to accomplish with your code!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.