简体   繁体   English

在主机应用和今日扩展之间共享用户生成的图像的最佳方法是什么

[英]What is the best way to share user generated images between host app and today extension

I know that to share data between main app and widget is to use NSUserDefaults or CoreData, but both of them are not recommended way to share images. 我知道要在主应用程序和小部件之间共享数据是使用NSUserDefaults或CoreData,但不建议两者都使用共享图像的方式。 To store user generated images , app should use app's Documents directory but it isn't accessible from widget, then how should the app share the images with it's widget? 要存储用户生成的图像 ,应用程序应使用应用程序的Documents目录,但无法从窗口小部件访问它,那么应用程序应如何与其窗口小部件共享图像?

I used shared app group directory to save the image and access it from both container app as well as extension, here's the code. 我使用共享的应用程序组目录保存图像,并从容器应用程序和扩展名访问它,这是代码。

Save image 保存图片

 public func saveImage(image:UIImage){        
    let fileManager = FileManager.default
    let directory = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "yourappgroup")?.appendingPathComponent("yourImagename.jpg")
    let imageData = UIImageJPEGRepresentation(image, 1)
    fileManager.createFile(atPath: (directory?.path)!, contents: imageData, attributes: nil)
}

Get saved image 获取保存的图像

public func saveImage() -> UIImage {
let fileManager = FileManager.default
    if let imagePath = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "yourappgroup")?.appendingPathComponent("yourImagename.jpg").path {            
        if fileManager.fileExists(atPath: imagePath){                
            return UIImage(contentsOfFile: imagePath)
        }else{
            print("No Image")                
            return nil
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Swift中的应用和今日扩展之间共享核心数据数据库 - Share Core Data database between app and today extension in Swift 无法在应用程序和今日扩展之间共享数据 - Can't share data between app and today extension (iOS Swift)在应用程序和Today Extension之间共享EKEventStore和访问 - (iOS Swift) Share EKEventStore and access between app and Today Extension 如何在 iOS 中的扩展和主机应用程序之间共享后删除数据 - How to remove data after share between extension and host app in iOS iOS Today Extension - 在之间共享核心数据 - iOS Today Extension - Share core data between iOS - 与多个应用程序目标共享今日扩展? - iOS - Share a Today Extension with multiple app targets? iOS 8共享扩展程序:是否存在回调可用于了解在显示共享扩展程序时用户是否退出了主机应用程序? - iOS 8 Share extension: is there callback available to know if user quit the host app while the share extension is being presented? "在应用扩展中检测方向的最佳方法是什么?" - What is the best way to detect orientation in an app extension? 除了“ UserDefaults”之外,还有什么其他方法可以在iOS中的“今日小工具扩展”和主“容器应用程序”之间共享数据 - Is there any other way than `UserDefaults` to share data between `Today Widget Extension` and the main `Container Application` in iOS 在iOS8中的App和App Extension之间共享用户对象 - Share User Object between App and App Extension in iOS8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM