iOS 退到后台 分享获取截屏

之前项目需要,做了一个shareExtension。但有一个bug一直没发现,就是截屏后截图还未入库,点击分享后,未拿到截图。所以未能在分享前和分享相册图片一样展示出来。
网上方法很多,但大多都是在应用内获取截屏,应用退到后台后该如何获取呢?

这里先简单的说一下应用内截屏图片的获取,文末会简单的说一下退到后台,后的分享操作

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
      //监听截屏通知
      [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(takeScreenshot:)
                                                 name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}
- (void) takeScreenshot:(NSNotification *)notification
{
   NSData *imageData = [self dataWithScreenshotInPNGFormat];
    UIImage *image = [UIImage imageWithData: imageData];
   //do you want to do……
}
//获取屏幕截图
- (NSData *)dataWithScreenshotInPNGFormat
{
    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();
    return UIImagePNGRepresentation(image);
}

//这是程序在应用内获取截屏图片,但微信,QQ在退到后台后,截屏点击分享仍然能过获取到,这里说一下

 for (NSExtensionItem *obj in self.extensionContext.inputItems) {
                for (NSItemProvider *itemProvider in obj.attachments) {
                    if ([itemProvider hasItemConformingToTypeIdentifier:@"public.image"])
                    {
                        [itemProvider loadItemForTypeIdentifier:@"public.image" options:nil completionHandler:^(id<NSSecureCoding>  _Nullable item, NSError * _Null_unspecified error) {
                            if ([(NSObject *)item isKindOfClass:[NSURL class]])
                            {
                            //从相册中分享,此时图片已经在相册中,取到的是路径Url
                                NSURL *imageUrl = (NSURL *)item;
                                NSString *url = imageUrl.absoluteString;
                                //do you want to do……
                            }else{
                                //截屏后点击分享,此时图片还未入库,所以拿到的是Image
                                UIImage *image = (UIImage *)item;
                               //do you want to do……
                        }];
                    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AGI阅读 16,018评论 3 119
  • 弟弟并不是那么需要我借给他钱,而是我需要给他支持。爱人的举动,我感觉到了他的理解与支持。理解了婆婆的保守。瞬间花儿...
    肖婷_81c4阅读 203评论 0 0
  • 腾讯企业邮箱: mxbiz1.qq.com mxbiz2.qq.com 网易企业邮箱设置: 1.输入以下 MX 记...
    fan12阅读 2,459评论 0 1
  • 今天是腊月二十九,午饭后我去起明叔家串门,家长里短的闲聊。 言谈之间 ,说起了牲口,起明叔说驴没有骡子好...
    赵彦国阅读 797评论 3 0
  • # 自我介绍 # 【编号】007-8918 【姓名】任颖松 【城市】惠州 【职业】自由人 【公众号/简书】吃素环保...
    吃素环保爱地球阅读 300评论 0 0