如何在通过共享扩展选择的NSUserDefaults中存储多个图像

时间:2017-01-31 07:45:03

标签: ios swift image swift2.3 share-extension

我目前为单张图片做得很好。但我想在app中选择多个图像并显示。任何人都可以帮助我。谢谢。

db.floorplan_backup.find().forEach(function (doc1) {
    var doc2 = db.floorplan.find({ userid: doc1.userid }, { firstviewhost: 1 });
    if (doc2 != null) {
        doc1.firstviewhost = doc2.firstviewhost;
        db.floorplan_backup.save(doc1);
    }
});
  

将图像保存为用户默认值

override func didSelectPost() {

        if let content = extensionContext!.inputItems[0] as? NSExtensionItem {
            let contentType = kUTTypeImage as String

            if let contents = content.attachments as? [NSItemProvider] {
                for attachment in contents {
                    if attachment.hasItemConformingToTypeIdentifier(contentType) {
                        attachment.loadItemForTypeIdentifier(contentType, options: nil) { data, error in
                            let url = data as! NSURL
                                if let imageData = NSData(contentsOfURL: url) {

                                    self.saveImage(imageData)

                            }
                        }
                    }
                }
            }
        }

        // Unblock the UI.
        self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
    }
  

在应用中阅读

 func saveImage(imageData: NSData) {
            if let prefs = NSUserDefaults(suiteName: suiteName) {
                prefs.setObject(imageData, forKey: keyName)
            }
    }
  

我修改了这个数组,但它不起作用。

if let prefs = NSUserDefaults(suiteName: suiteName) {

            if let imageData = prefs.objectForKey(keyName) as? NSData {

                drawCard(imageData)
            }
        }
  

将图像保存为用户默认值。

override func didSelectPost() {

        if let content = extensionContext!.inputItems[0] as? NSExtensionItem {
            let contentType = kUTTypeImage as String

            if let contents = content.attachments as? [NSItemProvider] {
                for attachment in contents {
                    if attachment.hasItemConformingToTypeIdentifier(contentType) {
                        attachment.loadItemForTypeIdentifier(contentType, options: nil) { data, error in
                            let url = data as! NSURL
                                if let imageData = NSData(contentsOfURL: url) {

                                    galleryBucket[url] = imageData
                                    self.saveImage(galleryBucket)

                            }
                        }
                    }
                }
            }
        }

        // Unblock the UI.
        self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
    }
  

这里我为galleryBucket

获得了nil
func saveImage(galleryBucket: [NSURL:NSData]) {
    if let prefs = NSUserDefaults(suiteName: suiteName) {
        prefs.setObject(galleryBucket, forKey: keyName)
    }
}

1 个答案:

答案 0 :(得分:1)

UserDefaults似乎不是保存图像的最佳位置..

看看这个: Save An Image To Application Documents Folder From UIView On IOS

这就是搜索特定文件扩展的方法。 Getting list of files in documents folder

一旦你有了名字,这就是你检索它的方式。 Get image from documents directory swift