iOS开发根据一个View截取图片(含超出一屏的View)

释放双眼,带上耳机,听听看~!

//根据一个View生成一个image

- (UIImage *)screenshotForView:(UIView *)view {

    UIImage *image = nil;

    //判断View类型(一般不是滚动视图或者其子类的话内容不会超过一屏,当然如果超过了也可以通过修改frame来实现绘制)

    if ([view.class isSubclassOfClass:[UIScrollView class]]) {

        UIScrollView *scrView = (UIScrollView *)view;




        CGPoint tempContentOffset = scrView.contentOffset;

        CGRect tempFrame = scrView.frame;




        scrView.contentOffset = CGPointZero;

        scrView.frame = CGRectMake(0, 0, scrView.contentSize.width, scrView.contentSize.height);




        image = [self screenshotForView:scrView size:scrView.frame.size];




        scrView.contentOffset = tempContentOffset;

        scrView.frame = tempFrame;




    } else {

        image = [self screenshotForView:view size:view.frame.size];

    }




    return image;

}

- (UIImage *)screenshotForView:(UIView *)view size:(CGSize)size {

    UIImage *image = nil;

    UIGraphicsBeginImageContextWithOptions(size, YES, [UIScreen mainScreen].scale);

    [view.layer renderInContext:UIGraphicsGetCurrentContext()];




    image = UIGraphicsGetImageFromCurrentImageContext();




    UIGraphicsEndImageContext();

    return image;

}

 

人已赞赏
iOS文章

iOS开发之UIDocumentInteractionController

2020-1-19 16:02:50

iOS文章

iOS开发Https双向认证

2020-1-19 19:15:57

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索