iOS截图那些事

2 篇文章 0 订阅
1 篇文章 0 订阅

普通view截图

+ (UIImage *)gjj_viewScreenShot:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(view.frame.size.width, view.frame.size.height ), NO, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    /// 返回一个基于当前图形上下文的图片
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

    /// 移除栈顶的基于当前位图的图形上下文
    UIGraphicsEndImageContext();
    return viewImage;
}

tableView、collectionView等scrollView生成长截图

实现步骤

  1. 保存scrollView截取前的偏移量及Frame
  2. 计算出你要截取的长图的高度及宽度,即scrollView的contentSize,将scrollView.frame设成scrollView.contentSize
  3. 渲染出scrollView整体(上下文),截取当前scrollView生成Image
  4. 恢复scrollView的偏移量及Frame
+ (UIImage *)gjj_scrollViewScreenShot:(UIScrollView *)scrollView {
    UIImage *image = nil;

    UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, YES, [UIScreen mainScreen].scale);
    CGPoint saveContentOffset = scrollView.contentOffset;
    CGRect savedFrame = scrollView.frame;
    scrollView.contentOffset = CGPointZero;
    scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
    [scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
    scrollView.layer.contents = nil;
    image = UIGraphicsGetImageFromCurrentImageContext();
    scrollView.contentOffset = saveContentOffset;
    scrollView.frame = savedFrame;
    UIGraphicsEndImageContext();
    return image;
}

webView生成长截图

webView和scrollView的截图大同小异

+ (UIImage *)gjj_webViewScreenShot:(UIWebView *)webView {
    UIImage *image = nil;

    UIGraphicsBeginImageContextWithOptions(webView.scrollView.contentSize, YES, [UIScreen mainScreen].scale);
    CGPoint saveContentOffset = webView.scrollView.contentOffset;
    CGRect savedFrame = webView.frame;
    webView.scrollView.contentOffset = CGPointZero;
    webView.frame = CGRectMake(0, 0, webView.scrollView.contentSize.width, webView.scrollView.contentSize.height);
    [webView.layer renderInContext:UIGraphicsGetCurrentContext()];
    webView.layer.contents = nil;
    image = UIGraphicsGetImageFromCurrentImageContext();
    webView.scrollView.contentOffset = saveContentOffset;
    webView.frame = savedFrame;
    UIGraphicsEndImageContext();
    return image;
}

图片拼接

很多需求是截图附带二维码等进行分享。如图:(截图增加了头部信息和底部二维码)
这里写图片描述

+ (UIImage *)gjj_addHeadImage:(UIImage *)headImage footImage:(UIImage *)footImage toMasterImage:(UIImage *)masterImage {
    CGSize size;
    size.width = masterImage.size.width;

    CGFloat headProportion = !headImage?0:(headImage.size.width / headImage.size.height);
    CGFloat footProportion = !footImage?0:(footImage.size.width / footImage.size.height);

    CGFloat headHeight = !headImage? 0:masterImage.size.width/headProportion;
    CGFloat footHeight = !footImage? 0:footImage.size.width/footProportion;

    size.height = masterImage.size.height + headHeight + footHeight;

    UIGraphicsBeginImageContextWithOptions(size, YES, 0.0);

    if (headImage) {
        [headImage drawInRect:CGRectMake(0, 0, masterImage.size.width, headHeight)];
    }
    [masterImage drawInRect:CGRectMake(0, headHeight, masterImage.size.width, masterImage.size.height)];
    if (footImage) {
        [footImage drawInRect:CGRectMake(0, masterImage.size.height + headHeight, masterImage.size.width, footHeight)];
    }
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
    return resultImage;
}

添加水印

这里写图片描述

+ (UIImage *)gjj_addWaterImage:(UIImage *)waterImage toMasterImage:(UIImage *)masterImage waterImageRect:(CGRect)rect {
    UIGraphicsBeginImageContextWithOptions(masterImage.size, NO, 0);
    [masterImage drawInRect:CGRectMake(0, 0, masterImage.size.width, masterImage.size.height)];
    /// 绘制水印图片到当前上下文
    [waterImage drawInRect:rect];

    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值