iOS google地图截屏

2018-06-14  本文已影响50人  酒深巷子Ya

最近在项目中处理即时通信 发送定位信息时用到地图截屏的功能,本以为是件很简单的事,结果还是踩了一些坑,Mark一下:

- (nullable UIImage *)snapshotToImage:(CGSize)size {
    self.mapView.myLocationEnabled = NO;
    CGPoint map_center = self.mapView.center;
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
    [self.mapView drawViewHierarchyInRect:CGRectMake(0, -(map_center.y - size.height), self.mapView.bounds.size.width, self.mapView.bounds.size.height) afterScreenUpdates:YES];
    UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return snapshotImage;
}

这个方法高德地图也可以使用,其它各种试图都可以使用此方法来截图。why? 因为这个方法:

- (BOOL)drawViewHierarchyInRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates NS_AVAILABLE_IOS(7_0);

该方法会按照层级来渲染当前view上下文的所有试图。
官方给出的解释:
Use this method to render a snapshot of the view hierarchy into the current context.
Returns NO if the snapshot is missing image data, YES if the snapshot is complete.
Calling this method from layoutSubviews while the current transaction is committing will
capture what is currently displayed regardless if afterUpdates is YES.

上一篇 下一篇

猜你喜欢

热点阅读