提高 CATiledView 的性能

发布于 2024-09-08 08:16:40 字数 1731 浏览 10 评论 0原文

我一直在寻找一种方法来提高我的 CATiledView 的性能。该视图几乎占据了整个屏幕(我有 iPad 和 iPhone 版本)。目前,图块完全填满需要大约 12 秒(两种设备上都需要 12 秒)。我见过其他应用程序使用 CATiledView,并且填充图块所需的时间要少得多。

我一直使用的图像是 1 7000x5000 JPEG。我尝试将图像宽度减小到 5500px,并且加载时间没有变化,因此我相信这是我的代码的问题,如下所示:

更新:我尝试减小图像的大小(通过更多超过一半),我看不出有什么区别。

-(void) loadMap{

        ...
        UIImage *tempImage = [UIImage imageWithData:data];
        self.image = tempImage;

        CGRect pageRect = CGRectMake(0,  0,  image.size.width, image.size.height);

        CATiledLayer *tiledLayer = [CATiledLayer layer];
        tiledLayer.anchorPoint = CGPointMake(0.0f, 1.0f);
        tiledLayer.delegate = self;
        tiledLayer.levelsOfDetail = 6; 
        tiledLayer.levelsOfDetailBias = 0;
        tiledLayer.bounds = pageRect; 
        tiledLayer.transform = CATransform3DMakeScale(1.0f, -1.0f, 0.3f);
        CGRect imageRect  = CGRectMake(0, 0, image.size.width, image.size.height);
        myContentView = [[UIView alloc] initWithFrame:imageRect];
        [myContentView.layer addSublayer:tiledLayer];

        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.chartView.frame];
        scrollView.delegate = self;
        scrollView.contentSize = pageRect.size;
        scrollView.minimumZoomScale = .2;   
        scrollView.maximumZoomScale = 1; 

        scrollView.clipsToBounds = YES;
        [scrollView addSubview:myContentView];
        [self.chartView addSubview:scrollView];
        scrollView.zoomScale = .2;


}

    - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
    {


        CGRect imageRect = CGRectMake (0.0, 0.0, image.size.width, image.size.height);
        CGContextDrawImage (ctx, imageRect, [image CGImage]);

    }

I have been looking for a way to improve the performance of my CATiledView. The view takes up nearly the entire screen (i have an iPad and iPhone version).Currently it takes about 12 seconds for the tiles to fill in completely (12 seconds on both devices). I have seen other apps use CATiledView and it take much less time for the tiles to fill.

The image I have been using is 1 7000x5000 JPEG. I have tried reducing the image doen to 5500px across and there is no change in the load time so that leads me to believe it is a problem with my code which is as follows:

update: I tried reducing the size of the images (by more than half) and there is no difference that I can tell.

-(void) loadMap{

        ...
        UIImage *tempImage = [UIImage imageWithData:data];
        self.image = tempImage;

        CGRect pageRect = CGRectMake(0,  0,  image.size.width, image.size.height);

        CATiledLayer *tiledLayer = [CATiledLayer layer];
        tiledLayer.anchorPoint = CGPointMake(0.0f, 1.0f);
        tiledLayer.delegate = self;
        tiledLayer.levelsOfDetail = 6; 
        tiledLayer.levelsOfDetailBias = 0;
        tiledLayer.bounds = pageRect; 
        tiledLayer.transform = CATransform3DMakeScale(1.0f, -1.0f, 0.3f);
        CGRect imageRect  = CGRectMake(0, 0, image.size.width, image.size.height);
        myContentView = [[UIView alloc] initWithFrame:imageRect];
        [myContentView.layer addSublayer:tiledLayer];

        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.chartView.frame];
        scrollView.delegate = self;
        scrollView.contentSize = pageRect.size;
        scrollView.minimumZoomScale = .2;   
        scrollView.maximumZoomScale = 1; 

        scrollView.clipsToBounds = YES;
        [scrollView addSubview:myContentView];
        [self.chartView addSubview:scrollView];
        scrollView.zoomScale = .2;


}

    - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
    {


        CGRect imageRect = CGRectMake (0.0, 0.0, image.size.width, image.size.height);
        CGContextDrawImage (ctx, imageRect, [image CGImage]);

    }

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

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

发布评论

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

评论(1

瀟灑尐姊 2024-09-15 08:16:40

我在今年 WWDC 的一个视频中找到了答案和示例代码。如果你有访问权限,请查看它,我相信它是 104,称为 Scroll Views

I found the answer and sample code in one of this years WWDC videos. If you have access, check it out it's 104 I believe, called Scroll Views

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