【问题标题】:Screenshot/Snapshot from mapView stores a blank Map来自 mapView 的屏幕截图/快照存储空白地图
【发布时间】:2012-07-01 01:39:20
【问题描述】:

我想截取 MapView 的屏幕截图并保存到照片中

这是使用的来源:

- (IBAction)screenshot:(id)sender {



if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    UIGraphicsBeginImageContextWithOptions(mapView.frame.size, NO, [UIScreen mainScreen].scale);

else


UIGraphicsBeginImageContext(mapView.frame.size);
[mapView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

}

动作成功,但是这里的照片是这样的 MapView Screenshot

我不知道出了什么问题。我已经尝试了一些代码。所有的结果都是一样的。如果我把整个视图截屏,地图也和上图一样。

有没有人有任何想法或可以帮助我?

【问题讨论】:

标签: objective-c ios xcode uiimage mkmapview


【解决方案1】:

编辑:

 - (UIImage*) ImageFromMapView
{
  UIGraphicsBeginImageContext(self.frame.size);
  [self.layer renderInContext:UIGraphicsGetCurrentContext()];
  //[[[yourmapView.layer sublayers] objectAtIndex:1] renderInContext:UIGraphicsGetCurrentContext()];  try this upper fails
  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();  
  return image;
}

尝试使用 UIGraphicsBeginImageContextWithOptions 代替 UIGraphicsBeginImageContext:

 UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0.0);

注意:从 iOS 4 开始,UIGraphicsBeginImageContextWithOptions 允许您提供比例因子。零比例因子将其设置为设备主屏幕的比例因子。这使您能够获得最清晰、分辨率最高的显示器快照,包​​括 Retina 显示器。

【讨论】:

  • 我已将代码更改为: if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) UIGraphicsBeginImageContextWithOptions(mapView.frame.size, NO, 0.0); // [UIScreen mainScreen].scale);否则 UIGraphicsBeginImageContextWithOptions(mapView.frame.size, NO, 0.0); [mapView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage * viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);但它总是相同的结果。 :-(
  • 看起来仍然是一个错误——无法抓取已保存的 MKMapView 图像。
  • 编辑不会改变任何东西。经过一些实验和更多的 SO 冲浪,需要发生两件事:1)您需要将图像保存在 MKMapView 的 mapViewDidFinishLoading 委托方法中。貌似调用这个方法后,需要延迟1-2秒后进行保存
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多