ios - 像 iMessage 这样指示位置的缩略图 URL 最好是什么?

标签 ios objective-c url thumbnails

<分区>

我需要一个可点击的缩略图来指示位置,就像 iMessage 一样,实现它的最佳方法是什么?

enter image description here

最佳答案

您可以将 MKMapView 添加到您的表格单元格中,但更好的方法是使用 MKSnapshotter 创建快照并将它们添加到您的单元格中。

来自 NSHipster 的博文 here ,从 MKMapSnapshotter 检索图像的示例代码是:

MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
options.region = self.mapView.region;
options.size = self.mapView.frame.size;
options.scale = [[UIScreen mainScreen] scale];

NSURL *fileURL = [NSURL fileURLWithPath:@"path/to/snapshot.png"];

MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
[snapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
    if (error) {
        NSLog(@"[Error] %@", error);
        return;
    }

    UIImage *image = snapshot.image;
    NSData *data = UIImagePNGRepresentation(image);
    [data writeToURL:fileURL atomically:YES];
}];
此处传递给快照的

选项MKMapSnapshotOptions。阅读博客以获取更多信息和完整教程。

MKMapSnapshotter 也足够灵活,可以向快照添加默认/自定义注释,正如您在问题中发布的那样。

来自同一来源的示例代码,带有注释:

[snapshotter startWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
              completionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
      if (error) {
          NSLog(@"[Error] %@", error);
          return;
      }

      MKAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:nil];

      UIImage *image = snapshot.image;
      UIGraphicsBeginImageContextWithOptions(image.size, YES, image.scale);
      {
          [image drawAtPoint:CGPointMake(0.0f, 0.0f)];

          CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
          for (id <MKAnnotation> annotation in self.mapView.annotations) {
              CGPoint point = [snapshot pointForCoordinate:annotation.coordinate];
              if (CGRectContainsPoint(rect, point)) {
                  point.x = point.x + pin.centerOffset.x -
                                (pin.bounds.size.width / 2.0f);
                  point.y = point.y + pin.centerOffset.y -
                                (pin.bounds.size.height / 2.0f);
                  [pin.image drawAtPoint:point];
              }
          }

          UIImage *compositeImage = UIGraphicsGetImageFromCurrentImageContext();
          NSData *data = UIImagePNGRepresentation(compositeImage);
          [data writeToURL:fileURL atomically:YES];
      }
      UIGraphicsEndImageContext();
}];

这里要注意的是,您必须使用自定义代码从快照创建图像。

生成快照后,您可以在单元格内绘制它。

如果您想进一步优化这一点,您可以将生成的快照保存在本地存储中,并将其用于相同坐标的单元格重用,而不是每次重用单元格时都生成图像。

阅读MKMapSnapshotter 的官方文档HEREMKSnapshotOptions HERE .

关于ios - 像 iMessage 这样指示位置的缩略图 URL 最好是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38517292/

相关文章:

objective-c - 垃圾收集在 LLVM 3 中重新定义?

iphone - 我正在播放在后台模式下触发的音乐 - 但它会在几秒钟后停止,为什么?

javascript - 在 WordPress 中根据 Url 隐藏菜单 Div 类

ios - Voip 服务 (PKPush) 委托(delegate)方法未被调用

iphone - 游戏中心导致 UIApplicationInvalidInterfaceOrientation

html - 在iOS中使UIWebview的字体加粗

ios - 在 Objective-C 中使用 Swift 类

ios - SpriteKit - 生成随机节点

http - 如何使用 Tuckey URL 重写从一个 URL 重定向到另一个 URL?

javascript - 使用javascript更改iframe中的URL