美文网首页
iOS 根据一个View截取图片(含超出一屏的View)

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

作者: 紫玉 | 来源:发表于2018-01-17 20:19 被阅读0次

    //根据一个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 根据一个View截取图片(含超出一屏的View)

          本文链接:https://www.haomeiwen.com/subject/mszuoxtx.html