GIF图片作为iOS版Google地图中的标记(Swift)

问题描述 投票:0回答:1

我有这个gif图片: enter image description here 我可以使用以下代码添加自定义静态图像作为标记:

if let location = locationManager.location {
            currentLocationMarker = GMSMarker(position: location.coordinate)
            currentLocationMarker?.icon = UIImage(named: "user")
            currentLocationMarker?.map = mapView
            currentLocationMarker?.rotation = locationManager.location?.course ?? 0
        }

我想用这个gif图像作为标记。反正呢?虽然我知道静态图像是鼓励但有可能吗?

我想添加一个UIView说一个100x100视图和动画这个图标,没有尝试过,但我尝试了这个线程中提到的解决方案:How to load GIF image in Swift? as:

let jeremyGif = UIImage.gifImageWithName("puppy")
let imageView = UIImageView(image: jeremyGif)
imageView.frame = CGRect(x: 0, y: 0, width: jeremyGif?.size.width ?? 100.0, height: jeremyGif?.size.height ?? 100.0)
//view.addSubview(imageView)
//mapView.addSubview(imageView)

if let location = locationManager.location {
    currentLocationMarker = GMSMarker(position: location.coordinate)
    currentLocationMarker?.icon = imageView.image //UIImage(named: "user")
    currentLocationMarker?.map = mapView
    currentLocationMarker?.rotation = locationManager.location?.course ?? 0
}

但这样做也行不通。我在资产中添加了这个gif图像,这就是它出现在它中的方式:

enter image description here

ios swift google-maps google-maps-markers cllocationmanager
1个回答
1
投票

我懂了。所以这是解决方案:

  1. 在项目文件夹中添加不在资产中的gif图像。 enter image description here
  2. 创建一个新的swift文件并从此链接复制/粘贴代码:https://github.com/kiritmodi2702/GIF-Swift/blob/master/GIF-Swift/iOSDevCenters%2BGIF.swift
  3. 将此代码添加到要获取gif图像的位置。在我的情况下,我将它放在用户位置: func addCurrentLocationMarker() { currentLocationMarker?.map = nil; currentLocationMarker = nil let puppyGif = UIImage.gifImageWithName("Puppy") let imageView = UIImageView(image: puppyGif) imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 100) if let location = locationManager.location { currentLocationMarker = GMSMarker(position: location.coordinate) currentLocationMarker?.iconView = imageView currentLocationMarker?.map = mapView currentLocationMarker?.rotation = locationManager.location?.course ?? 0 } }

它起作用:enter image description here


0
投票

谢谢你的问题和答案。这里的主要思想是使用显示Gif Image的UIImageView覆盖GMSMarker.iconView

我将你的想法用于我的实现,这可能比你的答案有点复杂但不使用第三个库

首先,我通过这个工具https://onlinepngtools.com/convert-gif-to-png将gif图像导出到png图像序列

然后我创建了动画那些PNG的UIImageView

UIImageView * gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
gifImageView.animationImages = @[ [UIImage imageNamed:@"gifToPngFrame01"]
                                  ,[UIImage imageNamed:@"gifToPngFrame02"]
                                  ,[UIImage imageNamed:@"gifToPngFrame03"]
                                  ,[UIImage imageNamed:@"gifToPngFrame04"]
                                  ,[UIImage imageNamed:@"gifToPngFrame05"]
                                  ,[UIImage imageNamed:@"gifToPngFrame06"]
                                 ,[UIImage imageNamed:@"gifToPngFrame07"]
                                 ,[UIImage imageNamed:@"gifToPngFrame08"]
                                 ,[UIImage imageNamed:@"gifToPngFrame09"]
                                 ,[UIImage imageNamed:@"gifToPngFrame10"]
                                 ,[UIImage imageNamed:@"gifToPngFrame11"]
                                 ,[UIImage imageNamed:@"gifToPngFrame12"]
                                 ,[UIImage imageNamed:@"gifToPngFrame13"]];

gifImageView.animationRepeatCount = 0;
gifImageView.animationDuration = 1;


CLLocationCoordinate2D position2 = CLLocationCoordinate2DMake(21.033333, 105.87);

GMSMarker *gifMarker = [GMSMarker markerWithPosition:position2];
gifMarker.iconView = gifImageView;
gifMarker.map = mapView;
gifMarker.groundAnchor = CGPointMake(0.5, 0.5);

[mapView animateToLocation:position2];

[gifImageView startAnimating];

此代码使用目标c。但我认为这可能会给其他人带来额外的好处

© www.soinside.com 2019 - 2024. All rights reserved.