【发布时间】:2016-12-06 02:57:42
【问题描述】:
在应用程序扩展中,有没有一种方法可以获取从存储在 /var/mobile/Containers/Data/Application//Documents// 文件夹中的包含应用程序生成的图像?
【问题讨论】:
标签: ios filesystems ios-app-extension
在应用程序扩展中,有没有一种方法可以获取从存储在 /var/mobile/Containers/Data/Application//Documents// 文件夹中的包含应用程序生成的图像?
【问题讨论】:
标签: ios filesystems ios-app-extension
为了使文件可用于应用扩展,您必须使用Group Path,因为应用扩展无法访问应用的文档文件夹,因此您必须按照以下步骤操作,
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;
}
您可以根据生成的图像扩展添加或更改路径扩展。
注意 - 请记住,您需要在组文件夹中而不是在文档文件夹中生成图像,因此它适用于应用程序和扩展程序。
干杯。
Swift 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!)
}
【讨论】: