加载整个图像并使用 CATiledLayer

发布于 2024-09-07 13:48:25 字数 135 浏览 2 评论 0原文

我不确定我是否很好地理解了苹果文档中的内容。我正在考虑使用 CATiledLayer 来显示 JPEG 图像。然而,我只有整个 JPEG 文件可供使用,没有小图块。是否仍然可以使用 CATiledLayer 并让它“平铺”JPEG?

谢谢!

I'm not sure I did understand that very well in the Apple doc. I'm considering using CATiledLayer to display a JPEG image. However, I only have an entire JPEG file at my disposal and no small tiles. Is it still possible to use a CATiledLayer and let it "tile" the JPEG?

Thanks!

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

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

发布评论

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

评论(3

自演自醉 2024-09-14 13:48:26

不可以。不幸的是,您必须自己贴瓷砖。 Core Animation 上的WWDC 2010 视频讨论了如何执行此操作并在其示例代码中,他们演示了当图块已经存在时如何使用 CATileLayer。

更正

我的意思是观看滚动视图会话。第 104 节“使用滚动视图设计应用程序”

No. You will have to tile them yourself, unfortunately. The WWDC 2010 videos on Core Animation discuss how to do this and in their sample code, they demonstrate how to use a CATileLayer when the tiles already exist.

Correction

I meant to say watch the Scroll View session. It's session 104 "Designing Apps with Scroll Views"

山人契 2024-09-14 13:48:26

您可以使用它在滚动视图中制作具有图像和缩放功能的 CATileLayer。

- (void)viewDidLoad
{
    [super viewDidLoad];

    // You do not need to and must not autorelease [NSBundle mainBundle]
    NSString *path = [[NSBundle mainBundle] pathForResource:@"img" ofType:@"jpg"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    image = [UIImage imageWithData:data];

    //    CGRect pageRect = CGRectMake(0,  0,  1600, 2400);

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


    CATiledLayer *tiledLayer = [CATiledLayer layer];
    tiledLayer.delegate = self;
    tiledLayer.tileSize = CGSizeMake(256.0, 256.0);
    tiledLayer.levelsOfDetail = 5; // was 1000, which makes no sense. Each level of detail is a power of 2.
    tiledLayer.levelsOfDetailBias = 5; // was 1000, which also makes no sense.
    // Do not change the tiledLayer.frame.
    tiledLayer.bounds = pageRect; // I think you meant bounds instead of frame.
    // Flip the image vertically.
    tiledLayer.transform = CATransform3DMakeScale(1.0f, -1.0F, 1.0f);

    myContentView = [[UIView alloc] initWithFrame:CGRectMake(500,400,image.size.width,image.size.height)];
    [myContentView.layer addSublayer:tiledLayer];

    //    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(10, 10, 300, 400)];
    scrollView.backgroundColor = [UIColor whiteColor];
    //  [scrollView setContentSize:CGSizeMake(image.size.width,image.size.height)];
    [scrollView setContentSize:image.size];
    scrollView.maximumZoomScale = 32; // = 2 ^ 5
    scrollView.delegate = self;
    //    scrollView.contentSize = pageRect.size;


    [scrollView addSubview:myContentView];

    [self.view addSubview:scrollView];

}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return myContentView;
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"img" ofType:@"jpg"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    image = [UIImage imageWithData:data];
    CGRect imageRect = CGRectMake (0.0, 0.0, image.size.width, image.size.height);

    CGContextDrawImage (context, imageRect, [image CGImage]);
}

You may use This for making CATileLayer with image and Zoom functionality in scrollview.

- (void)viewDidLoad
{
    [super viewDidLoad];

    // You do not need to and must not autorelease [NSBundle mainBundle]
    NSString *path = [[NSBundle mainBundle] pathForResource:@"img" ofType:@"jpg"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    image = [UIImage imageWithData:data];

    //    CGRect pageRect = CGRectMake(0,  0,  1600, 2400);

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


    CATiledLayer *tiledLayer = [CATiledLayer layer];
    tiledLayer.delegate = self;
    tiledLayer.tileSize = CGSizeMake(256.0, 256.0);
    tiledLayer.levelsOfDetail = 5; // was 1000, which makes no sense. Each level of detail is a power of 2.
    tiledLayer.levelsOfDetailBias = 5; // was 1000, which also makes no sense.
    // Do not change the tiledLayer.frame.
    tiledLayer.bounds = pageRect; // I think you meant bounds instead of frame.
    // Flip the image vertically.
    tiledLayer.transform = CATransform3DMakeScale(1.0f, -1.0F, 1.0f);

    myContentView = [[UIView alloc] initWithFrame:CGRectMake(500,400,image.size.width,image.size.height)];
    [myContentView.layer addSublayer:tiledLayer];

    //    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(10, 10, 300, 400)];
    scrollView.backgroundColor = [UIColor whiteColor];
    //  [scrollView setContentSize:CGSizeMake(image.size.width,image.size.height)];
    [scrollView setContentSize:image.size];
    scrollView.maximumZoomScale = 32; // = 2 ^ 5
    scrollView.delegate = self;
    //    scrollView.contentSize = pageRect.size;


    [scrollView addSubview:myContentView];

    [self.view addSubview:scrollView];

}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return myContentView;
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"img" ofType:@"jpg"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    image = [UIImage imageWithData:data];
    CGRect imageRect = CGRectMake (0.0, 0.0, image.size.width, image.size.height);

    CGContextDrawImage (context, imageRect, [image CGImage]);
}
绮烟 2024-09-14 13:48:26

更简单的方法是缩小图像直到适合(我认为设备最多支持 2044x2044 左右)。您可以使用 CGImageCreateWithImageInRect() 创建 CGImage 的子图像

The easier way is to just downscale the image until it fits (I think devices support up to 2044x2044 or so). You can create subimages of a CGImage with CGImageCreateWithImageInRect()

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