I am trying to display an animated gif in an overlay for MKMapView
. The overlay is created using the MKOverlayRenderer
. To animate the gif in iOS 7, I'm using the UIImage+animatedGIF
category posted here on GitHub.
The image of the animated gif displays fine in the overlay using the category; however, the gif does not animate. I have no problem using the category to animate a gif in a UIImageView
but it does not seem to work correctly in a map view overlay.
How can I use this category to place an animated gif in a map view overlay?
or...
Is there a way to place a UIImageView
in the overlay which might solve my problem by setting the UIImageView
with the animated gif?
My overlay renderer subclass is the following:
MapOverlayRenderer.h
#import <MapKit/MapKit.h>
@interface MapOverlayRenderer : MKOverlayRenderer
- (instancetype)initWithOverlay:(id<MKOverlay>)overlay overlayImage:(UIImage *)overlayImage;
@end
MapOverlayRenderer.m
#import "MapOverlayRenderer.h"
@interface MapOverlayRenderer ()
@property (strong,nonatomic) UIImage *image;
@end
@implementation MapOverlayRenderer
- (instancetype)initWithOverlay:(id<MKOverlay>)overlay overlayImage:(UIImage *)overlayImage {
self = [super initWithOverlay:overlay];
if (self) {
_image = overlayImage;
}
return self;
}
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
CGImageRef imageReference = self.image.CGImage;
MKMapRect theMapRect = [self.overlay boundingMapRect];
CGRect theRect = [self rectForMapRect:theMapRect];
CGContextScaleCTM(context, 1.0, -1.0);
CGContextTranslateCTM(context, 0.0, -theRect.size.height);
CGContextDrawImage(context, theRect, imageReference);
}
@end
In my UIViewController
, I am fetching the animated gif and adding the overlay by calling a method which contains the following code:
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:radarUrl] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
self.radarImage = [UIImage animatedImageWithAnimatedGIFData:data]; //for animated radar image
dispatch_async(dispatch_get_main_queue(), ^{
[self.mapView addOverlay:self.polygon];
});
}] resume];
Any suggestions on how to animate a gif in an iOS 7 map view overlay would be greatly appreciated.
Because a map view overlay is having your draw in a
CGContext
, it won't animate -- that is a buffer which is drawn to and translated to view contents, as opposed to being a normal part of the view hierarchy. Unfortunately you will need to make use of-setNeedsDisplayInMapRect:zoomScale:
repeatedly to request animations. This system is a bit less flexible than iOS 6 and before, which added normal views atop the map.