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.