无法在swift iOS 8 Extension中投射UIImage [英] Unable to cast UIImage in swift iOS 8 Extension

查看:167
本文介绍了无法在swift iOS 8 Extension中投射UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,我正在尝试构建一个动作扩展,它将从提供的图像中扫描条形码。这是代码。

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 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
        }
    }
}

现在每当我尝试获取UIImage时,我总是为零,而在图像中,我可以看到接收到的图像。但是当我尝试从该图像获取UIImage时,它总是返回nil。以下是可能有帮助的调试屏幕

Now, whenever I try to get UIImage I always get nil, whereas in the image I can see an image is received. But when I try to get UIImage from that image it always returns nil. Here is the screen shot from debugging that might help



更新:以下是收到的图片说明:

Update: Here is the description of the image that is received as :


打印图像描述:(NSSecureCoding!)image =
(instance_type = Builtin.RawPointer = 0x15d674c0 - > 0x32b6be5c(void
* )0x32b6be70:NSURL)

Printing description of image: (NSSecureCoding!) image = (instance_type = Builtin.RawPointer = 0x15d674c0 -> 0x32b6be5c (void *)0x32b6be70: NSURL)

我使用目标C创建了相同的扩展名并且它可以工作,但不是在swift中。这是客观的C代码:

I have created the same extension using objective C and it works, but not in swift. Here is 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 tries to search a lot on Google but found nothing. I even tried a changed code but doe not work, here is the changed 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)
                        }
                    }
                })

更新:我注意到了一件事;如果我创建一个新项目并为其添加一个动作扩展,则会自动生成相同的代码,除了我在块中添加的几行。即使在没有更改单行自动生成代码的情况下,imageView.image也是零。这是一个快速的错误?或者我的Xcode应用程序的一些错误。

Update: I have noticed one thing; if I create a new project and add an action extension to it, the same code is auto generated except few line that I added in the block. In that also without even changing a single line of auto generated code, the imageView.image is nil. Is this a bug in swift?? Or some bug with my Xcode app.

推荐答案

问题是图像不是 UIImage ,它是 NSURL

the thing is that image is not UIImage, it's NSURL.

将代码更改为此代码:

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

这篇关于无法在swift iOS 8 Extension中投射UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆