11

In iOS 7, MKOverlayView was replaced by MKOverlayRenderer. Before, I was able to add a UIImageView as a subview to MKOverlayView, and access the CALayer of the MKOverlayView. Now, without UIView gone in MKOverlayRenderer, I'm not sure on how to add a custom CALayer (I have a CAKeyFrameAnimation that goes through a series of pictures rapidly). How would I add a UIImageView into MKOverlayRenderer? How would I add a CALayer in a CGContextRef (used by MKOverlayRenderer)?

I tried doing it on my own, but it doesn't go through the images at all.

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
CGContextSaveGState(context);

CGRect test = [self rectForMapRect:self.overlay.boundingMapRect];
// Clip the drawing space to the map rect
CGRect clipRect = [self rectForMapRect:mapRect];

CGContextClipToRect(context, clipRect);

// CoreGraphics' coordinate system is flipped, so we need to transform it first
CGContextScaleCTM(context, 1.0, -1.0);
CGContextTranslateCTM(context, 0, -test.size.height);

// Draw the portion of the image in the map rect

// CGContextDrawImage(context, test, [image CGImage]);

CGContextRestoreGState(context);

CALayer *sublayer = [CALayer layer];
sublayer.frame = test;
NSArray *radarImages = [NSArray arrayWithObjects:(id)image.CGImage, image1.CGImage, image2.CGImage, image3.CGImage, image4.CGImage, image5.CGImage, image6.CGImage, image7.CGImage, image8.CGImage, image9.CGImage, image10.CGImage, image11.CGImage, image12.CGImage, image13.CGImage, image14.CGImage, image15.CGImage, image16.CGImage, image17.CGImage, image18.CGImage,image19.CGImage,image20.CGImage,image21.CGImage, image22.CGImage, image23.CGImage, image24.CGImage, nil];


anim = [CAKeyframeAnimation animation];
[anim setKeyPath:@"contents"];
[anim setCalculationMode:kCAAnimationDiscrete];
[anim setValues:radarImages];
[anim setRepeatCount:INFINITY];
[anim setDuration:2.6];
[self drawLayer:sublayer inContext:context];
[sublayer addAnimation:anim forKey:nil];
[sublayer renderInContext:context];
[sublayer setNeedsDisplay];

}

Any help is appreciated, thanks!

Unheilig
  • 16,196
  • 193
  • 68
  • 98
sridvijay
  • 1,526
  • 18
  • 39
  • I'm having the same problem, http://stackoverflow.com/questions/20558591/animated-gif-not-working-in-mkmapview-overlay-using-mkoverlayrenderer?noredirect=1#comment30789828_20558591. Hopefully someone on Stack Overflow will help us. Please let me know if you figure this out. – wigging Dec 21 '13 at 02:38
  • Maybe try calling -setNeedsDisplayInMapRect:zoomScale: on a timer to mark it dirty so your drawMapRect gets called again. – MindJuice Dec 25 '13 at 08:37
  • any updates here? – David Seek May 29 '17 at 01:25

1 Answers1

-1

Try adding the following code

     [[[self view] layer] addSublayer: sublayer];

and use NSMutableArray instead of NSArray.

Nauman
  • 11
  • 5