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;
}
其實重點還是截屏區域座標的確定


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章