0

I currently did for single image which is working fine. But i want to pick multiple images and display in app.Can any one please help me in this. Thank you.

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)
    }

Saves an image to user defaults

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

Reading in app

if let prefs = NSUserDefaults(suiteName: suiteName) {

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

                drawCard(imageData)
            }
        }

And i modified this for array, but its not working.

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)
    }

Saves an image to user defaults.

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

Here I am getting nil for galleryBucket

if let prefs = NSUserDefaults(suiteName: suiteName) {

            if let galleryBucket = prefs.objectForKey(keyName) as? [NSURL:NSData] {

                for imageData in galleryBucket{

                    drawCard(imageData.1)
                }
            }
        }
shallowThought
  • 19,212
  • 9
  • 65
  • 112
Chetan
  • 226
  • 3
  • 12
  • Why not just save an array of NSData for an array of images? – Benjamin Lowry Jan 31 '17 at 07:47
  • @BenjaminLowry Please check the edited question, is their anything i did wrong? – Chetan Jan 31 '17 at 07:56
  • I don't know what you are doing with `drawCard(imageData.1)` because I don't believe imageData is a tuple, but other than that it looks fine. The only thing I can think of that would cause it to be nil is if your `suiteName` or `keyName` variables changed. – Benjamin Lowry Jan 31 '17 at 07:59
  • drawCard(imageData.1) will dynamically create imageview, and suiteName and keyName are exactly same. – Chetan Jan 31 '17 at 09:14

1 Answers1

1

UserDefaults doesn't seem like the best place for saving images..

Take a look at this: Save An Image To Application Documents Folder From UIView On IOS

And this is how you can search for a specific file extenstion. Getting list of files in documents folder

And once you have a name, this is how you retrieve it. Get image from documents directory swift

Community
  • 1
  • 1