问题描述
我正在尝试在 MKMapView
的叠加层中显示动画 gif.叠加层是使用 MKOverlayRenderer
创建的.为了在 iOS 7 中为 gif 设置动画,我使用 UIImage+animatedGIF
类别发布 此处.
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.
动画 gif 的图像在使用类别的叠加层中显示良好;但是,gif 没有动画.我在 UIImageView
中使用该类别为 gif 设置动画没有问题,但它在地图视图叠加层中似乎无法正常工作.
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.
如何使用此类别在地图视图叠加层中放置动画 gif?
How can I use this category to place an animated gif in a map view overlay?
或...
有没有办法在覆盖层中放置一个 UIImageView
来解决我的问题,方法是用动画 gif 设置 UIImageView
?
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
MapOverlayRenderer.h
#import <MapKit/MapKit.h>
@interface MapOverlayRenderer : MKOverlayRenderer
- (instancetype)initWithOverlay:(id<MKOverlay>)overlay overlayImage:(UIImage *)overlayImage;
@end
MapOverlayRenderer.m
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
在我的 UIViewController
中,我通过调用包含以下代码的方法来获取动画 gif 并添加叠加层:
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];
关于如何在 iOS 7 地图视图叠加层中为 gif 设置动画的任何建议将不胜感激.
Any suggestions on how to animate a gif in an iOS 7 map view overlay would be greatly appreciated.
推荐答案
https://github.com/jhurray/iOS7AnimatedMapOverlay
这是在 iOS7 中为叠加层设置动画的最佳方式
this is the best way to animate overlays in iOS7
这篇关于动画 gif 在使用 MKOverlayRenderer 的 MKMapView 叠加层中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!