无法在swift iOS 8扩展中强制转换UIImage

无法在swift iOS 8扩展中强制转换UIImage,ios,objective-c,swift,ios8,ios-app-extension,Ios,Objective C,Swift,Ios8,Ios App Extension,我有一个奇怪的问题,我正试图建立一个行动扩展,将扫描条码的图像提供。这是代码 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 thi

我有一个奇怪的问题,我正试图建立一个行动扩展,将扫描条码的图像提供。这是代码

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。下面是调试的屏幕截图,可能会有所帮助

更新:以下是接收到的图像的描述:

图像打印说明:(NSSecureCodeding!)图像= (实例类型=Builtin.RawPointer=0x15d674c0->0x32b6be5c(无效) *)0x32b6be70:NSURL)

我使用objective C创建了相同的扩展,它可以工作,但在swift中不起作用。以下是目标C代码:

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

我试图在谷歌上搜索很多东西,但什么也没找到。我甚至尝试了一个更改后的代码,但不起作用,以下是更改后的代码:

                    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也是nil。这是斯威夫特的一只虫子吗??或者我的Xcode应用程序出现了一些错误。

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

将代码更改为此代码:

imageView.image = UIImage(data: NSData(contentsOfURL: image as NSURL)!)!
获取图像作为

<UIImage: 0x16ecb410>, {100, 100}
你需要这样做 为了澄清我添加了完整的代码,请参考,它为我工作
究竟为什么要使用异步调度?我们可能会收到很多图像,现在我只处理一个图像进行调试,所以在找到图像时使用break。因此,如果扩展接收到多个图像,我们可以异步扫描这些图像。您是否知道,您在Swift中发送的闭包与ObjC中的块不同?不,Joe,我想我可能需要进一步研究它。但是,如果你能解释两者的区别以及为什么这段代码不起作用;这将是一点指导。@JoJoe即使我将代码更改为使用block,imageToSet仍然为零。我正在用更改的代码更新问题。我将尝试一下并提供反馈。谢谢我有一个问题,Objective-C允许您直接从完成块参数获取图像数据,但Swift只返回296字节的NSSecureCodeding,这(自然)会在尝试从它创建图像时导致nil。你(Simon)在上面的URL方法方面有什么好运气吗?我会检查它是否解决了我的问题,并将我的发现发布在这里。是的,它是有效的,我也在努力解决这个问题。这就是照片应用程序的情况,我想知道是否会为我自己的应用程序中的照片提供一个url,因为每个人都无法访问该应用程序。我不敢相信很少有人看到这一点。。。这是最新的Xcode iOS操作模板中的一个bug。“,{100100}”
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
    }
}
}