我目前为单个图像做的工作正常。但我想选择多个图像并在应用程序中显示。任何人都可以帮助我。谢谢你。
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)
}
将图像保存为用户默认值。
func saveImage(galleryBucket: [NSURL:NSData]) {
if let prefs = NSUserDefaults(suiteName: suiteName) {
prefs.setObject(galleryBucket, forKey: keyName)
}
}
在这里,galleryBucket 我得到了 nil
if let prefs = NSUserDefaults(suiteName: suiteName) {
if let galleryBucket = prefs.objectForKey(keyName) as? [NSURL:NSData] {
for imageData in galleryBucket{
drawCard(imageData.1)
}
}
}