将大的 CATiledLayer 渲染成更小的区域

发布于 2024-10-29 18:17:37 字数 510 浏览 0 评论 0原文

我有一个尺寸为 4096 x 4096 的 CATiledLayer,我想将其渲染为尺寸为 1024 x 1024 的 PNG,

但这并没有做到...

-(NSData *)createPNGFormat
{
    UIGraphicsBeginImageContext(CGSizeMake(1024, 1024));
    tiledLayer.transform = CATransform3DMakeScale(0.25, 0.25, 1.0);
    [tiledLayer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return UIImagePNGRepresentation(image);     

}

关于如何做到这一点有什么想法吗?

I have a CATiledLayer of size 4096 x 4096 which I want to render into a PNG of size 1024 x 1024

this isnt doing it...

-(NSData *)createPNGFormat
{
    UIGraphicsBeginImageContext(CGSizeMake(1024, 1024));
    tiledLayer.transform = CATransform3DMakeScale(0.25, 0.25, 1.0);
    [tiledLayer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return UIImagePNGRepresentation(image);     

}

Any ideas on how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫以 2024-11-05 18:17:37

[First Hack] 暴力破解,逐块渲染。这有效(缓慢)

-(UIImage *)renderTileX:(NSInteger)xpos tileY:(NSInteger)ypos scale:(CGFloat)ascale
{
    CGSize tiledsize = tiledLayer.tileSize;
    CGRect tiledframe = tiledLayer.bounds;
    CALayer *container = [CALayer layer];
    container.frame = CGRectMake(0, 0, tiledsize.width,tiledsize.height);
    UIGraphicsBeginImageContext(tiledsize);
    [container addSublayer:tiledLayer];
    tiledLayer.frame = CGRectMake(-tiledsize.width*xpos,-tiledsize.height*ypos, tiledframe.size.width, tiledframe.size.height);
    [container renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [tiledLayer removeFromSuperlayer];

    CGFloat fsize = tiledsize.width*ascale;
    CGRect apicrect = CGRectMake(0,0,fsize,fsize);

    UIGraphicsBeginImageContext(apicrect.size);
    [image drawInRect:apicrect];
    UIImage *thumb = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return thumb;   


}


-(UIImage *)renderTiledLayer
{

    UIGraphicsBeginImageContext(CGSizeMake(1024, 1024));

    for (NSInteger x = 0 ; x<4; x++) {
        for (NSInteger y = 0; y < 4; y++) {
            NSLog(@"render %d:%d",x,y);
            UIImage *tile = [self renderTileX:x tileY:y scale:0.25];
            [tile drawAtPoint:CGPointMake(x*256, y*256)];
        }
    }
    UIImage *fimage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();


    return fimage;      
}

[使用 Google...] 更好的方法

-(UIImage *)createThumb
{

    UIGraphicsBeginImageContext(CGSizeMake(1024, 1024));
    CGContextScaleCTM(UIGraphicsGetCurrentContext(), 0.25, 0.25);
    [tiledLayer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *fimage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return fimage;  
}

[First Hack] Brute Force , Render tile by tile. This works (slowly)

-(UIImage *)renderTileX:(NSInteger)xpos tileY:(NSInteger)ypos scale:(CGFloat)ascale
{
    CGSize tiledsize = tiledLayer.tileSize;
    CGRect tiledframe = tiledLayer.bounds;
    CALayer *container = [CALayer layer];
    container.frame = CGRectMake(0, 0, tiledsize.width,tiledsize.height);
    UIGraphicsBeginImageContext(tiledsize);
    [container addSublayer:tiledLayer];
    tiledLayer.frame = CGRectMake(-tiledsize.width*xpos,-tiledsize.height*ypos, tiledframe.size.width, tiledframe.size.height);
    [container renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [tiledLayer removeFromSuperlayer];

    CGFloat fsize = tiledsize.width*ascale;
    CGRect apicrect = CGRectMake(0,0,fsize,fsize);

    UIGraphicsBeginImageContext(apicrect.size);
    [image drawInRect:apicrect];
    UIImage *thumb = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return thumb;   


}


-(UIImage *)renderTiledLayer
{

    UIGraphicsBeginImageContext(CGSizeMake(1024, 1024));

    for (NSInteger x = 0 ; x<4; x++) {
        for (NSInteger y = 0; y < 4; y++) {
            NSLog(@"render %d:%d",x,y);
            UIImage *tile = [self renderTileX:x tileY:y scale:0.25];
            [tile drawAtPoint:CGPointMake(x*256, y*256)];
        }
    }
    UIImage *fimage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();


    return fimage;      
}

[Using the Google...] The better way

-(UIImage *)createThumb
{

    UIGraphicsBeginImageContext(CGSizeMake(1024, 1024));
    CGContextScaleCTM(UIGraphicsGetCurrentContext(), 0.25, 0.25);
    [tiledLayer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *fimage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return fimage;  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文