Unable to start UIImage in iOS 8 quick extension

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

imageToSetConsoleThe complete shot

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.

+6
source share
3 answers

the fact is that the image is not UIImage , it is NSURL .

Change the code to this:

 imageView.image = UIImage(data: NSData(contentsOfURL: image as NSURL)!)! 
+7
source

U need to do this:

  if let strongImageView = weakImageView { if let imageURL = image as? NSURL{ strongImageView.image = UIImage(data:NSData(contentsOfURL: imageURL)!) }else{ strongImageView.image = image as? UIImage } } 

For clarification, I added the full code. Please contact, it worked for me.

 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 } } } 
+2
source

Image acquisition as

 <UIImage: 0x16ecb410>, {100, 100} 

It cannot be distinguished as NSURL and get nil in the following expression.

 imageView.image = UIImage(data: NSData(contentsOfURL: image as NSURL)!)! 
0
source

Source: https://habr.com/ru/post/975410/


All Articles