3
在应用程序扩展中,有一种方法可以从包含应用程序中生成的图像生成,该应用程序存储在/ var/mobile/Containers/Data/Application // Documents //夹?应用程序扩展如何访问包含应用程序中的文件Doucments /文件夹
在应用程序扩展中,有一种方法可以从包含应用程序中生成的图像生成,该应用程序存储在/ var/mobile/Containers/Data/Application // Documents //夹?应用程序扩展如何访问包含应用程序中的文件Doucments /文件夹
为了使文件提供给应用程序的扩展,你必须使用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;
}
您可以添加或更改路径扩展按照你生成图像扩展。
注意 - 请记住,您需要在组文件夹中生成图像,而不是在文档文件夹中生成图像,因此它可用于应用程序和扩展程序。
干杯。
斯威夫特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!)
}
非常感谢你这完全适用!现在我需要将所有图像传输到AppGroup // Documents /文件夹。 –