之前项目需要,做了一个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……
}];
}