iOS 手机截屏

百度地图自带截图功能,可以截取路线列表,保存到本地。可是对比发现截下来的图片并不是app中看到的那样,截图中头部加入了搜索的起点和终点,每段路程的详细站点都已展开,而且图片会根据路线的长短自动判断图片的长度。于是猜想,百度地图的截图思路是:

Step1:创建当前列表的头视图用于展示起始位置信息,获得视图1。

Step2:重新创建新的站点列表视图,并将详细站点展开,获得视图2.

Step3:创建容器视图(UIView或UIScrollView),将视图1与视图2放到容器视图中使用下面的方法截取视图创建图片。

Step4:保存图片到本地相册,Over。

截屏的方法如下:

//获得屏幕图像
- (UIImage *)imageFromView: (UIView *) theView
{
    
    UIGraphicsBeginImageContext(theView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [theView.layer renderInContext:context];
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return theImage;
}

//获得某个范围内的屏幕图像
- (UIImage *)imageFromView: (UIView *) theView   atFrame:(CGRect)r
{
    UIGraphicsBeginImageContext(theView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    UIRectClip(r);
    [theView.layer renderInContext:context];
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return  theImage;//[self getImageAreaFromImage:theImage atFrame:r];
}

 

posted @ 2017-03-07 16:05  PaulpauL  阅读(242)  评论(2编辑  收藏  举报