views:

670

answers:

1

I have a layer backed view, I am trying to add subLayers roughly sized around 300 X 270 (in pixels ) to it, its that the, sublayers count may reach upto the 1000 to 2000 in number and not to mention each of sublayer is again scalable to roughly 4280 X 1500 or more for starters.

So the problem is obviously that of a GPU constraint.

after adding around 100 subLayers sized 300 X 270 , there is a warning " image is too large for GPU, ignoring" and that messing up with my the layer display. solution for such problem (from some mailing lists) was to use CATiledLayer , but I can't make use of the tiledLayer due to the complex requirement of the subLayer's display.

Is there a possibility of removingthe subLayers which don't fall under VisibleRect of the view ?

I tried to "removeFromSuperlayer" and then adding it whenever required , there's always a crash when I try to add the subLayer back.

Is there any solution for tackling the problem ?

any help is appreciated.

I am adding sublayer twice (i need to change it) but for now just for the gist of code

-(IBAction)addLayer:(id)sender { Layer *l = [[Layer alloc] init]; CALayer *layer = [l page]; [contentArray addObject:page]; [drawLayer addSublayer:layer]; [self layout]; }

-(void)layout { NSEnumerator *pageEnumr = [contentArray objectEnumerator];

float widthMargin = [self frame].size.width;
CGRect rect;
float zoom = [self zoomFactor];
while(obj = [contentEnmr nextObject] )
{
 [obj setZoomFactor:zoom];
 CALayer *pg =(CALayer *)[obj page] ;
 rect = pg.bounds;

 if ( x + pg.bounds.size.width   > widthMargin  )
 {
  x = xOffset;
  y += rect.size.height + spacing ;

 }
 rect.origin = CGPointMake(x,y);
 [obj changeBounds];

 NSRect VisibleRect = [self visibleRect];
 NSRect result = NSIntersectionRect(VisibleRect,NSRectFromCGRect( rect));
 if( NSEqualRects (result ,NSZeroRect) )
 {
  [pg removeFromSuperlayer];
 }else
 {
  [drawLayer addSublayer:pg];
  [pg setFrame:rect];
  [pg setNeedsDisplay];
 }


 x += ( rect.size.width + spacing);
}

NSRect viewRect = [self frame];
if(viewRect.size.height < ( y + rect.size.height + spacing )  )
 viewRect.size.height = ( y + rect.size.height + spacing) ;

[self setFrameSize: viewRect.size];

}

@interface Layer : NSObject { CALayer *page; } @property (retain) CALayer *page;

Thanks Rajesh

A: 

Have a look at the PhotoScroller application included as part of the WWDC conference. It demonstrates how to zoom and scroll through a very large image by loading only portions of that image that are currently visible.

Also check out this discussion.

Paul Alexander