I have a strange problem, I am trying to build an action extension that will scan barcode from the image provided. Here is the code.
override func viewDidLoa
Getting image as
<UIImage: 0x16ecb410>, {100, 100}
It cannot be casted as NSURL
and getting nil
in the following expression.
imageView.image = UIImage(data: NSData(contentsOfURL: image as NSURL)!)!
if let strongImageView = weakImageView {
if let imageURL = image as? NSURL{
strongImageView.image = UIImage(data:NSData(contentsOfURL: imageURL)!)
}else{
strongImageView.image = image as? UIImage
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Get the item[s] we're handling from the extension context.
// For example, look for an image and place it into an image view.
// Replace this with something appropriate for the type[s] your extension supports.
var imageFound = false
for item: AnyObject in self.extensionContext!.inputItems {
let inputItem = item as! NSExtensionItem
for provider: AnyObject in inputItem.attachments! {
let itemProvider = provider as! NSItemProvider
if itemProvider.hasItemConformingToTypeIdentifier(kUTTypeImage as String) {
// This is an image. We'll load it, then place it in our image view.
weak var weakImageView = self.imageView
itemProvider.loadItemForTypeIdentifier(kUTTypeImage as String, options: nil, completionHandler: { (image, error) in
NSOperationQueue.mainQueue().addOperationWithBlock {
if let strongImageView = weakImageView {
if let imageURL = image as? NSURL{
strongImageView.image = UIImage(data:NSData(contentsOfURL: imageURL)!)
}else{
strongImageView.image = image as? UIImage
}
}
}
})
imageFound = true
break
}
}
if (imageFound) {
// We only handle one image, so stop looking for more.
break
}
}
}
the thing is that image is not UIImage
, it's NSURL
.
Change code to this one:
imageView.image = UIImage(data: NSData(contentsOfURL: image as NSURL)!)!