开发者

Load entire image and use CATiledLayer

I'm not sure I did understand that very well in the Apple doc. I'm considering using CATiledLayer to display a JPEG image. However, I only have an entire JPEG file at my disposal and no smal开发者_StackOverflow社区l tiles. Is it still possible to use a CATiledLayer and let it "tile" the JPEG?

Thanks!


No. You will have to tile them yourself, unfortunately. The WWDC 2010 videos on Core Animation discuss how to do this and in their sample code, they demonstrate how to use a CATileLayer when the tiles already exist.

Correction

I meant to say watch the Scroll View session. It's session 104 "Designing Apps with Scroll Views"


You may use This for making CATileLayer with image and Zoom functionality in scrollview.

- (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]);
}


The easier way is to just downscale the image until it fits (I think devices support up to 2044x2044 or so). You can create subimages of a CGImage with CGImageCreateWithImageInRect()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜