首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >加载整个图像并使用CATiledLayer

加载整个图像并使用CATiledLayer
EN

Stack Overflow用户
提问于 2010-06-29 08:05:03
回答 3查看 2.7K关注 0票数 1

我不确定我在苹果文档中是否理解得很好。我正在考虑使用CATiledLayer来显示JPEG图像。但是,我只有一个完整的JPEG文件可供我使用,而且没有小的磁贴。还有没有可能使用CATiledLayer并让它“瓦片”JPEG?

谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-06-30 10:14:46

不是的。不幸的是,您将不得不自己平铺它们。核心动画上的WWDC 2010 videos讨论了如何做到这一点,在他们的示例代码中,他们演示了如何在磁贴已经存在的情况下使用CATileLayer。

校正

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

票数 1
EN

Stack Overflow用户

发布于 2011-07-12 18:07:05

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

代码语言:javascript
复制
- (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]);
}
票数 1
EN

Stack Overflow用户

发布于 2010-06-29 20:55:57

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3137011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档