菜鸟教程小白 发表于 2022-12-11 19:39:49

ios - QLThumbnailProvider 扩展似乎没有被调用


                                            <p><p>我正在开发一个 QLThumbnailProvider 扩展来显示我的文档类型的缩略图。我的扩展程序没有<strong>似乎</strong>被调用 - 我的缩略图没有出现,而且我没有看到我添加的日志记录出现在任何日志文件中。</p>

<p>我有一个基于 UIDocumentBrowserViewController 的应用程序,它定义了一种新的文档类型。它导出一个 UTI (<code>com.latenightsw.Eureka.form</code>)。我的应用能够浏览、创建和打开文档,但缩略图是空白的。</p>

<p>我已将缩略图扩展目标添加到我的项目中。代码如下所示:</p>

<pre><code>class ThumbnailProvider: QLThumbnailProvider {
    override func provideThumbnail(for request: QLFileThumbnailRequest, _ handler: @escaping (QLThumbnailReply?, Error?) -&gt; Void) {
      // Third way: Set an image file URL.
      print(&#34;provideThumbnail: \(request)&#34;)
      handler(QLThumbnailReply(imageFileURL: Bundle.main.url(forResource: &#34;EurekaForm&#34;, withExtension: &#34;png&#34;)!), nil)

    }
}
</code></pre>

<p>我已确认 <code>EurekaForm.png</code> 是目标的一部分,并被复制到扩展程序包(以及主机应用程序包)。</p>

<p>我已经确认我的尿路感染已被宣布:</p>

<p> <a href="/image/U8K03.png" rel="noreferrer noopener nofollow"><img src="/image/U8K03.png" alt="Thumbnail Extension PList"/></a> </p>

<p>有人有什么建议吗? </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>似乎日志记录和断点有时在应用程序扩展中不起作用。甚至 <code>fatalError</code> 都会默默发生。</p>

<p>在我的项目中,我无法让初始化程序 <code>QLThumbnailReply(imageFileURL:)</code> 工作。然而,其他初始化程序似乎工作得更好。</p>

<h1>将图像绘制到上下文中</h1>

<p>使用上下文初始化器时,您必须使用介于 <code>request.minimumSize</code> 和 <code>request.maximumSize</code> 之间的上下文大小。</p>

<p>下面我编写了一些代码,它获取图像并将其绘制到上下文中,同时保持上述条件。</p>

<pre><code>override func provideThumbnail(for request: QLFileThumbnailRequest, _ handler: @escaping (QLThumbnailReply?, Error?) -&gt; Void) {

    let imageURL = // ... put your own code here

    let image = UIImage(contentsOfFile: imageURL.path)!


    // size calculations

    let maximumSize = request.maximumSize
    let imageSize = image.size

    // calculate `newImageSize` and `contextSize` such that the image fits perfectly and respects the constraints
    var newImageSize = maximumSize
    var contextSize = maximumSize
    let aspectRatio = imageSize.height / imageSize.width
    let proposedHeight = aspectRatio * maximumSize.width

    if proposedHeight &lt;= maximumSize.height {
      newImageSize.height = proposedHeight
      contextSize.height = max(proposedHeight.rounded(.down), request.minimumSize.height)
    } else {
      newImageSize.width = maximumSize.height / aspectRatio
      contextSize.width = max(newImageSize.width.rounded(.down), request.minimumSize.width)
    }

    handler(QLThumbnailReply(contextSize: contextSize, currentContextDrawing: { () -&gt; Bool in
      // Draw the thumbnail here.

      // draw the image in the upper left corner
      //image.draw(in: CGRect(origin: .zero, size: newImageSize))

      // draw the image centered
      image.draw(in: CGRect(x: contextSize.width/2 - newImageSize.width/2,
                              y: contextSize.height/2 - newImageSize.height/2,
                              width: newImageSize.width,
                              height: newImageSize.height);)

      // Return true if the thumbnail was successfully drawn inside this block.
      return true
    }), nil)
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - QLThumbnailProvider 扩展似乎没有被调用,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/48451557/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/48451557/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - QLThumbnailProvider 扩展似乎没有被调用