封装程序员日记恩美第二个APP项目

iOS的截图生成图片分享到微信朋友、微博等

2017-09-12  本文已影响85人  9d8c8692519b

在iOS的开发中,经常会遇到分享到微信、微博时需要附带一张分享图片,图片中需要分享的相关信息(如APP下载二维码等)。

实现思路:

我们以View为示例,将要分享的内容布局到上面。通过对这个View的截屏获得需要分享的图片。然后拿到截图去分享即可完成上述需求。当然以此类推 对UIimageView、UIWebview等都可以用相同思路来实现。

截图部分实现代码:

#import "UIView+snapshot.h"

@implementation UIView (UIView_Snapshot)

-(UIImage*)snapshot:(UIView *)shotView {
    UIGraphicsBeginImageContext(shotView.bounds.size);
    UIGraphicsBeginImageContextWithOptions(shotView.bounds.size, NO, [[UIScreen mainScreen] scale]);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.layer renderInContext:context];
    UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return screenShot;
}
@end

实现的截图(image)效果如下:

截图.jpeg

附:

你可能需要了解UIWebView的&WKWebView如何截图? 你可以前往这里查看并获取demo

上一篇 下一篇

猜你喜欢

热点阅读