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 /文件夾。 –