Converting NSSecureCoding to NSData - Xcode - Swift

心已入冬 提交于 2019-12-10 16:13:01

问题


I have some code that creates an NSSecureCoding variable called "content" and I want to convert that variable into NSData that can then be made into a UIImage or be sent to a local server. How do I convert this properly? I want this for a Share Extension I am making in my iOS app, so when you press share on a photo, it gets the photo contents and converts it into NSData. Here is my code:

    inputItem = extensionContext!.inputItems.first as NSExtensionItem
    attachment = inputItem.attachments![0] as NSItemProvider
    if (attachment.hasItemConformingToTypeIdentifier(kUTTypeImage as String)){
        attachment.loadItemForTypeIdentifier(kUTTypeImage as String,
            options: nil,
            completionHandler: {(content, error: NSError!) in

                //insert code to convert "content"(NSSecureCoding) to NSData variable

        })
    }

回答1:


kinda late but this happened to me today and I solved it like this, inside the completionHandler:

  if let data = content {
        self.imageData = UIImage(data: NSData(contentsOfURL: data as! NSURL)!)                      
  }

imageData is UIImage type.



来源:https://stackoverflow.com/questions/29477544/converting-nssecurecoding-to-nsdata-xcode-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!