在应用程序扩展中,有没有一种方法可以获取从存储在 /var/mobile/Containers/Data/Application//Documents// 文件夹中的包含应用程序生成的图像?
为了使文件可用于应用程序扩展,您必须使用Group Path
,由于应用程序扩展无法访问应用程序的文档文件夹,因此您必须按照以下步骤操作,
- Enable 应用程序组 from 项目设置->功能.
- 添加组扩展,例如
group.yourappid
.
-
然后使用以下代码。
NSString *docPath=[self groupPath];
NSArray *contents=[[NSFileManager defaultManager] contentsOfDirectoryAtPath:docPath error:nil];
NSMutableArray *images=[[NSMutableArray alloc] init];
for(NSString *file in contents){
if([[file pathExtension] isEqualToString:@"png"]){
[images addObject:[docPath stringByAppendingPathComponent:file]];
}
}
-(NSString *)groupPath{
NSString *appGroupDirectoryPath = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:group.yourappid].path;
return appGroupDirectoryPath;
}
您可以根据生成的图像扩展名添加或更改路径扩展名。
注意 - 请记住,您需要在组文件夹中而不是在文档文件夹中生成图像,因此它可用于应用程序和扩展程序。
Cheers.
斯威夫特 3 更新
let fileManager = FileManager.default
let url = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "YOUR_GROUP_ID")?.appendingPathComponent("logo.png")
// Write to Group Container
if !fileManager.fileExists(atPath: url.path) {
let image = UIImage(named: "name")
let imageData = UIImagePNGRepresentation(image!)
fileManager.createFile(atPath: url.path as String, contents: imageData, attributes: nil)
}
// Read from Group Container - (PushNotification attachment example)
// Add the attachment from group directory to the notification content
if let attachment = try? UNNotificationAttachment(identifier: "", url: url!) {
bestAttemptContent.attachments = [attachment]
// Serve the notification content
self.contentHandler!(self.bestAttemptContent!)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)