加载整个图像并使用CATiledLayer

时间:2010-06-29 00:05:03

标签: iphone image core-animation jpeg

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

谢谢!

3 个答案:

答案 0 :(得分:1)

没有。不幸的是,你必须自己平铺它们。核心动画上的WWDC 2010 videos讨论了如何执行此操作,并且在示例代码中,它们演示了如何在已存在切片时使用CATileLayer。

<强>校正

我想说看滚动视图会话。会议104“使用滚动视图设计应用程序”

答案 1 :(得分: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]);
}

答案 2 :(得分:0)

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

创建CGImage的子图像