Ios 使用选项字典加载预览图像

Ios 使用选项字典加载预览图像,ios,swift,Ios,Swift,我正在编写一个iOS应用程序共享扩展,我想获得一个大的预览图像。经过一些努力,我能够使这段代码正常工作: class ShareViewController: SLComposeServiceViewController { // This is the result handler for the call to loadPreviewImageWithOptions let imageHandler: NSItemProviderCompletionHand

我正在编写一个iOS应用程序共享扩展,我想获得一个大的预览图像。经过一些努力,我能够使这段代码正常工作:

class ShareViewController: SLComposeServiceViewController {

    // This is the result handler for the call to loadPreviewImageWithOptions        
    let imageHandler: NSItemProviderCompletionHandler = { [unowned self]
        (result: NSSecureCoding?, error: NSError!) in

        if result is UIImage
        {
            let image = result as! UIImage

            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.imageView.image = image
                self.imageView.contentMode = UIViewContentMode.ScaleAspectFill
                self.imageView.clipsToBounds = true
            })
        }
    }

    // Find the shared item and preview it.
    for item: AnyObject in self.extensionContext!.inputItems
    {
        let inputItem = item as! NSExtensionItem
        for provider: AnyObject in inputItem.attachments!
        {
            let provider = provider as! NSItemProvider

            // I want a preview image as large as the device.
            var options_dict = [NSObject:AnyObject]()
            options_dict[NSItemProviderPreferredImageSizeKey] = NSValue(CGSize: CGSize(width: 960, height:540))

            provider.loadPreviewImageWithOptions(options_dict, completionHandler: imageHandler)
        }
    }

...
}
我获得了一幅图像,但它的大小始终是84 x 79像素。从选项字典中,应支持预览图像大小:

选项-键和值的字典,提供有关项的信息,例如图像的大小。有关可能键的列表,请参见选项字典键

并在同一页的“选项”下键入:

NSItemProviderPreferredImageSizeKey- 以像素为单位指定图像尺寸的键。此键的值是包含CGSize或NSSize数据类型的NSValue对象

有一个线索:

键在传递给NSItemProviderLoadHandler块的options参数的字典中使用


因此,也许我必须使用size选项调用或覆盖loadItemForTypeIdentifier,然后调用loadPreviewImageWithOptions?我现在正在试这个。

你让它起作用了吗??我也试着这么做!