2010-06-29 46 views
1

我不确定我在Apple文档中的确很好理解。我正在考虑使用CATiledLayer来显示JPEG图像。不过,我只有一个完整的JPEG文件,并且没有小块。是否仍然可以使用CATiledLayer并让它“平铺”JPEG?加载整个图像并使用CATiledLayer

谢谢!

+0

顺便说一句,我纠正了我的答案。这是我打算推荐的Scroll View会话。 – 2010-06-30 16:20:31

回答

1

不可以。不幸的是,您必须自己平铺它们。 Core Animation上的WWDC 2010 videos讨论了如何做到这一点,并在他们的示例代码中演示了当tile已经存在时如何使用CATileLayer。

修正

我的意思是说看滚动浏览会话。这是会话104“使用滚动视图设计应用程序”

+0

谢谢。事实上,通过观看这一部分,我想知道如果没有现有的瓷砖,这是可能的。 我想显示一个图像集合存储为JPEG。我应该在运行时平铺它们吗?我应该只是不瓷砖他们... 这是否意味着在照片应用程序,他们存储的图片瓷砖除了图片? – Kamchatka 2010-06-30 20:48:56

+0

如果您使用随Photoshop附带的Zoomify插件导出图像,它生成的图块非常适合与CAtiledLayer一起使用。您可能需要批量重命名它们以满足您的需求,但尺寸效果相当不错。只要删除它生成的.swf并只使用这些图像。 – 2012-10-18 21:05:41

0

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

1

您可以使用此功能在滚动视图中使用图像和缩放功能制作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]); 
}