iOS截屏并修改截图然后分享的功能实现

一. 实现的效果类似微博的截图分享

不仅截图分享的时候还进行图片的修改,增加自己的二维码

二.实现方式

苹果在ios7之后提供了一个新的通知类型:UIApplicationUserDidTakeScreenshotNotification,

这个通知会告知注册了此通知的对象已经发生了截屏事件,然后我们就可以在这个事件中实现自己的逻辑

1.注册通知

- (void)viewDidLoad {
    [super viewDidLoad];
    //注册用户的截屏操作通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(userDidTakeScreenshot:)
                                                 name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}

2.接收通知 (获取截图并修改的图片,并展示,展示UI,可以自己修改)

//截屏响应
- (void)userDidTakeScreenshot:(NSNotification *)notification
{
    NSLog(@"检测到截屏");

    //人为截屏, 模拟用户截屏行为, 获取所截图片
    _testImg = [self imageWithScreenshot];

//    //添加显示
    UIImageView *imgvPhoto = [[UIImageView alloc]initWithImage:_testImg];
    imgvPhoto.frame = CGRectMake(0, WIN_HEIGHT/2, WIN_WIDTH/2, WIN_HEIGHT/2);
    imgvPhoto.backgroundColor = [UIColor orangeColor];
    imgvPhoto.userInteractionEnabled = YES;
    //添加边框
    CALayer * layer = [imgvPhoto layer];
    layer.borderColor = [[UIColor whiteColor] CGColor];
    layer.borderWidth = 5.0f;
    //添加四个边阴影
    imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor;
    imgvPhoto.layer.shadowOffset = CGSizeMake(0, 0);
    imgvPhoto.layer.shadowOpacity = 0.5;
    imgvPhoto.layer.shadowRadius = 10.0;
    //添加两个边阴影
    imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor;
    imgvPhoto.layer.shadowOffset = CGSizeMake(4, 4);
    imgvPhoto.layer.shadowOpacity = 0.5;
    imgvPhoto.layer.shadowRadius = 2.0;

    [self.view  addSubview:imgvPhoto];

    // 添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImgView:)];
    [imgvPhoto addGestureRecognizer:tap];
}

3. 截图并修改图片

/**
 *  截取当前屏幕 并修改
 *
 *  @return NSData *
 */
- (UIImage *)imageWithScreenshot
{
    CGSize imageSize = CGSizeZero;
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (UIInterfaceOrientationIsPortrait(orientation))
        imageSize = [UIScreen mainScreen].bounds.size;
    else
        imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);

    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    for (UIWindow *window in [[UIApplication sharedApplication] windows])
    {
        CGContextSaveGState(context);
        CGContextTranslateCTM(context, window.center.x, window.center.y);
        CGContextConcatCTM(context, window.transform);
        CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
        if (orientation == UIInterfaceOrientationLandscapeLeft)
        {
            CGContextRotateCTM(context, M_PI_2);
            CGContextTranslateCTM(context, 0, -imageSize.width);
        }else if (orientation == UIInterfaceOrientationLandscapeRight)
        {
            CGContextRotateCTM(context, -M_PI_2);
            CGContextTranslateCTM(context, -imageSize.height, 0);
        } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
            CGContextRotateCTM(context, M_PI);
            CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
        }
        if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
        {
            [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
        }
        else
        {
            [window.layer renderInContext:context];
        }
        CGContextRestoreGState(context);
    }

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // 修改图片
    NSData *imageData = UIImagePNGRepresentation(image);
    UIImage *LastImage = [UIImage imageWithData:imageData];

    UIImage *img = [UIImage imageNamed:@"ico_nursery.png"];
    CGImageRef imgRef = img.CGImage;
    CGFloat w = CGImageGetWidth(imgRef);
    CGFloat h = CGImageGetHeight(imgRef);

    //以1.png的图大小为底图
    UIImage *img1 = LastImage;
    CGImageRef imgRef1 = img1.CGImage;
    CGFloat w1 = CGImageGetWidth(imgRef1);
    CGFloat h1 = CGImageGetHeight(imgRef1);

    //以1.png的图大小为画布创建上下文
    UIGraphicsBeginImageContext(CGSizeMake(w1, h1 + 100));
    [img1 drawInRect:CGRectMake(0, 0, w1, h1)];//先把1.png 画到上下文中
    [img drawInRect:CGRectMake(10, h1 + 10, 80, 80)];//再把小图放在上下文中
    UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();//从当前上下文中获得最终图片
    UIGraphicsEndImageContext();//关闭上下文

    return  resultImg;
}

4.根据添加的事件进行分享 分享自己也可封装

// 点击图片改变imageView位置,打印图片信息  分享自己也可封装
- (void)tapImgView: (UITapGestureRecognizer *)tap {

    NSLog(@"点击了图片...");
 // 微信
    [MyAPIClient mobEvent:@"wechat"];
//  [Helper shareImageName:_testImg type:SSDKPlatformSubTypeWechatSession];//  微信好友
    [Helper shareImageName:_testImg type:SSDKPlatformSubTypeWechatTimeline];// 微信朋友圈
// QQ
//    [MyAPIClient mobEvent:@"QQ"];
//    [Helper shareImageName:_testImg type:SSDKPlatformTypeQQ];// QQ

}

5. 移除通知

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

这样就可以了.展示一下测试

自身截图

截图修改分享图

ok,结束,需要补充的,欢迎大家留言讨论!

原文地址:https://www.cnblogs.com/ljcgood66/p/9435537.html

时间: 2024-08-05 16:19:48

iOS截屏并修改截图然后分享的功能实现的相关文章

IOS截屏,View截图的基本方法

IOS截屏的方法网上有很多,以下是我个人认为比较好的一个,我稍微改了一点 来源:SDScreenshotCapture #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) UIImage *getImageWithFullScreenshot(vo

IOS截屏

最近,在找IOS截屏的方法.找到一个,现在记录下来.跟我想的差不多,还是,进入操作(初始化),操作(复制当前屏幕,保存),退出.最小的操作结构. 1 . 先指定图像的大小 UIGraphicsBeginImageContext(view.frame.size); 2. 在指定的区域绘制图像 [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO]; 3. 获取图像上下文 UIImage *image = UIGraphics

ios 截屏

把当前屏幕作为获取成为图片 - (UIImage *)rn_screenshot {    UIGraphicsBeginImageContext(self.bounds.size);    [self.layer renderInContext:UIGraphicsGetCurrentContext()];    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext(

ios截屏代码[转]

http://www.cnblogs.com/chenxiangxi/p/3547974.html 这位博主的连接中将ios自定义大小位置的截屏代码写的很不错,马上就能用的方法,对于只想马上用的程序员很有帮助 http://www.2cto.com/kf/201310/250228.html 我将其改为以下代码: 1 #pragma mark -=====自定义截屏位置大小的逻辑代码=====- 2 static int ScreenshotIndex=0; //这里的逻辑直接采用上面博主的逻辑

iOS截屏代码

/** *截图功能 */ -(void)screenShot{ UIGraphicsBeginImageContextWithOptions(CGSizeMake(640, 960), YES, 0); //设置截屏大小 [[self.view layer] renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGrap

iOS 截屏以及相关扩展(UIImage的绘制和渲染)

1.截取当前屏幕 CGSize windowSize = behandView.bounds.size; UIGraphicsBeginImageContextWithOptions(windowSize, YES, 2.0); CGContextRef context = UIGraphicsGetCurrentContext(); [behandView.window.layer renderInContext:context]; UIImage *snapshot = UIGraphics

iOS截屏保存至相册

#pragma mark 截屏并保存至相册 -(void)screenShotsComplete:(void(^)(UIImage * img)) complete { CGSize imageSize = [[UIScreen mainScreen] bounds].size; UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); CGContextRef context = UIGraphicsGetCurrentContext(

ios 截屏(代码)

//1.首先在storyboard中拖一些控件,包括UIButton控件,将UIButton控件拖线到控制器中(方法.CutImage) //2.在CutImage方法中调用NSTimer方法 - (IBAction)CutImage:(UIButton *)sender { NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 targer:self  selector:@selector(clipImage) userInf

ios截屏使用的方法

- (UIImage*)getimage{//截屏使用的方法CGSize imageSize = [[UIScreen mainScreen] bounds].size;if (NULL != UIGraphicsBeginImageContextWithOptions) {UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);} else {UIGraphicsBeginImageContext(imageSize);}CGConte