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
})
}
DispatchQueue.global().async {
attachment.loadItem(forTypeIdentifier: kUTTypeImage as String, options: nil, completionHandler: { (item, error) in
if let error = error {
print(error.localizedDescription)
return
}
var image: UIImage?
if item is UIImage {
image = item as? UIImage
}
if item is URL {
let data = try? Data(contentsOf: item as! URL)
image = UIImage(data: data!)!
}
if item is Data {
image = UIImage(data: item as! Data)!
}
if let image = image {
DispatchQueue.main.async {
// image here
}
}
})
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.