OverlayRenderer在MKMapView覆盖中不起作用

OverlayRenderer在MKMapView覆盖中不起作用

本文介绍了使用MKOverlayRenderer在MKMapView覆盖中不起作用的动画gif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 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 ,这可能通过设置 UIImageView 使用动画gif?

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.

推荐答案

这是在iOS7中为叠加层制作动画的最佳方式

this is the best way to animate overlays in iOS7

这篇关于使用MKOverlayRenderer在MKMapView覆盖中不起作用的动画gif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 05:40