I have a strange problem, I'm trying to create an extension of an action that scans a barcode from the provided image. Here is the code.
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 NSString) { // This is an image. We'll load it, then place it in our image view. weak var weakImageView = self.imageView itemProvider.loadItemForTypeIdentifier(kUTTypeImage as NSString, options: nil, completionHandler: { (image, error) in if image != nil { dispatch_async(dispatch_get_main_queue(),{ if let imageView = weakImageView { var imageToSet: UIImage? = image as? UIImage imageView.image = image as? UIImage } self.imageToScan = self.imageView.image self.scanFromImage(self.imageToScan!) }) } }) imageFound = true break } } if (imageFound) { // We only handle one image, so stop looking for more. break } } }
Now, when I try to get UIImage, I always get zero, whereas in the image I see that the image is received. But when I try to get UIImage from this image, it always returns zero. Here is a screenshot from debugging that might help



Update: here is a description of the image, which is obtained as:
Printing an image description: (NSSecureCoding!) Image = (instance_type = Builtin.RawPointer = 0x15d674c0 β 0x32b6be5c (void *) 0x32b6be70: NSURL)
I created the same extension using target C, and it works, but not in fast. Here is the objective C code:
- (void)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. BOOL imageFound = NO; for (NSExtensionItem *item in self.extensionContext.inputItems) { for (NSItemProvider *itemProvider in item.attachments) { if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) { // This is an image. We'll load it, then place it in our image view. __weak UIImageView *imageView = self.imageView; [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:nil completionHandler:^(UIImage *image, NSError *error) { if(image) { dispatch_async(dispatch_get_main_queue(), ^{ [imageView setImage:image]; imageToScan = image; [self scanImage:imageToScan]; }); } }]; imageFound = YES; break; } } if (imageFound) { // We only handle one image, so stop looking for more. break; } }
}
I am trying to search a lot on Google but havenβt found anything. I even tried the modified code, but it didnβt work, here is the modified code:
itemProvider.loadItemForTypeIdentifier(kUTTypeImage as NSString, options: nil, completionHandler: { (image, error) in if image != nil { NSOperationQueue.mainQueue().addOperationWithBlock { if let imageView = weakImageView { var imageToSet: UIImage? = image as? UIImage imageView.image = image as? UIImage } self.imageToScan = self.imageView.image self.scanFromImage(self.imageToScan) } } })
Update: I noticed one thing: if I create a new project and add an action extension to it, the same code will be created automatically, except for a few lines that I added to the block. In this case, without even changing a single line of automatically generated code, imageView.image is zero. Is this a mistake fast? Or some bug with my Xcode application.