views:

1022

answers:

2

I'm currently having the following problem with CATiledLayer: when the view first loads, the scrolling works perfectly, but then when you zoom once, the view snaps to the anchor point (top left corner) and it can no longer scroll at all. The zooming works in that it will zoom in and out, but it will only zoom to the top left corner.

My code is as follows:

#import <QuartzCore/QuartzCore.h>
#import "PracticeViewController.h"

@implementation practiceViewController
//@synthesize image;

- (void)viewDidLoad
{


    NSString *path = [[NSBundle mainBundle] pathForResource:@"H-5" ofType:@"jpg"]; 
    NSData *data = [NSData dataWithContentsOfFile:path];
    image = [UIImage imageWithData:data];

    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.tileSize = CGSizeMake(1000, 1000);
    tiledLayer.levelsOfDetail = 6; 
    tiledLayer.levelsOfDetailBias = 0;
    tiledLayer.bounds = pageRect; 
    tiledLayer.transform = CATransform3DMakeScale(1.0f, -1.0f, 0.3f);

    myContentView = [[UIView alloc] initWithFrame:self.view.bounds];
    [myContentView.layer addSublayer:tiledLayer];

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.delegate = self;
    scrollView.contentSize = pageRect.size;
    scrollView.minimumZoomScale = .2;   
    scrollView.maximumZoomScale = 1; 
    [scrollView addSubview:myContentView];
    [self.view addSubview:scrollView];


}

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

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"H-5" 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 (ctx, imageRect, [image CGImage]);
}

@end
A: 

Here's my first pass at fixing the obvious bugs:

#import <QuartzCore/QuartzCore.h>
#import "PracticeViewController.h"

@implementation PracticeViewController
@synthesize image;

- (void)viewDidLoad
{
    [super viewDidLoad];

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

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

    CATiledLayer *tiledLayer = [CATiledLayer layer];
    tiledLayer.delegate = self;
    tiledLayer.tileSize = CGSizeMake(1024.0, 1024.0);
    tiledLayer.levelsOfDetail = 5; // was 1000, which makes no sense. Each level of detail is a power of 2.
    tiledLayer.levelsOfDetailBias = 0; // 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(zoom, -zoom, 1.0f);

    myContentView = [[UIView alloc] initWithFrame:self.view.bounds];
    [myContentView.layer addSublayer:tiledLayer];

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.delegate = self;
    scrollView.contentSize = pageRect.size;
    scrollView.maximumZoomScale = 32; // = 2 ^ 5
    [scrollView addSubview:myContentView];

    [self.view addSubview:scrollView];
}

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

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"H-5" 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]);
}

@end
lucius
thank you very much - please see my edit above. The view loads with the image now, but the scrolling and zooming doesn't work. When I zoom in or out, it automatically scrolls the view so that a certain point (perhaps the center oof the image but im not sure) is in the upper left corner. Also the tiles load slower than I had hoped (about 2 seconds per tile) is that about normal?
Brodie
also - the scrolling will work fine until I try to zoom, and as soon as I zoom in or out, the scrolling will not work at all
Brodie
Use image.size to get the image size. The tiles load very slow because it has to decompress the JPG for every tile the way the code is written now. You should either store the entire UIImage in memory and draw that, or slice it up into tiles, which is what I do in my Transit Maps iPhone app. Zooming is based on the tiledLayer.anchorPoint, which ranges from 0.0 to 1.0 for each axis. You need to set the anchor point to something like {0.5,0.5} for it to zoom from the center. You also need to set the tileLayer.position to the center of the screen.
lucius
Okay I have updated my code to reflect what I have right now, there is really only one problem now. When the view first loads, the scrolling works perfectly. But then when you zoom once, the view snaps to the anchor point (top left corner) and it can no longer scroll at all. The zooming works in that it will zoom in and out, but it will only zoom to the top left corner.
Brodie
A: 

I got the total same issue like you do. Till now ,i am still working on it ,and it is in the midnight here now. Mine are the pdf files to zoom in ,and it works perfect .But it zooms from left corner exactly...every time. and i can not scroll the view at all....so do you have any ideas about this issue ?

frank