1

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
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Brodie
  • 3,526
  • 8
  • 42
  • 61
  • 1
    Stack Overflow is not intended to be used for performing code reviews. If you have specific problems, ask questions related to those. – Brad Larson May 03 '10 at 22:57
  • I do have a specific problem which I have documented below in lucius's answer. "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. " not sure what your complaining about – Brodie May 04 '10 at 01:33
  • 4598 - There was no actual question in your question, and parsing the comments to figure out what was being asked is not very straightforward. I've edited your question to reflect what you are asking now. – Brad Larson May 04 '10 at 03:33
  • 1
    4598 - With the current edits, I've also marked your newer question: http://stackoverflow.com/questions/2750320/iphone-last-problem-with-catiled-layer-i-hope as a duplicate of this one, because it is asking the same thing. No offense intended, I'm just trying to keep things organized. – Brad Larson May 04 '10 at 03:37
  • Ok thank you. Just trying to get the last issue worked out. – Brodie May 05 '10 at 02:47

1 Answers1

0

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
  • 8,665
  • 3
  • 34
  • 41
  • 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 Apr 29 '10 at 21:41
  • 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 Apr 29 '10 at 21:49
  • 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 Apr 30 '10 at 06:44
  • 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 Apr 30 '10 at 16:25
  • 1
    it is very difficult to understand the solution provided for the listed problems. I am interested in only some aspects of the issue but I can't easily extract selective info from here. I want to know how you solved the issue with not being able to scroll after zoom – Radu Simionescu Feb 27 '14 at 14:16