iOS 地图截屏

上一次截屏的代码真是又丑又长, 看看大神是怎么写的吧
- (nullable UIImage *)generateSnapshot:(CGSize)size
{
    self.addressLabelBg.hidden = YES;
    self.addressLabel.hidden = YES;
    self.mapView.showsUserLocation = NO;

    CGPoint mapCenter = self.mapView.center;
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
    [self.mapView drawViewHierarchyInRect:CGRectMake(-(mapCenter.x - size.width / 2),
                                                     -(mapCenter.y - size.height / 2),
                                                     self.mapView.bounds.size.width,
                                                     self.mapView.bounds.size.height)
                       afterScreenUpdates:YES];
    UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    self.addressLabelBg.hidden = NO;
    self.addressLabel.hidden = NO;
    self.mapView.showsUserLocation = YES;

    return snapshotImage;
}
其实重点还是截屏区域坐标的确定


你可能感兴趣的:(IOS,UI,设计)