簡體   English   中英

在某些情況下,當從擴展中調用時,UIGraphicsImageRenderer 閉包不會完成並返回圖像

[英]UIGraphicsImageRenderer closure doesn't complete and return an image in some cases when invoked from within an extension

我正在嘗試在通知服務擴展收到推送時生成圖像(是的,在服務擴展中,而不是內容擴展中。我需要在收到用戶定向推送時生成圖像並添加到聯系人中)。 但是,我注意到在某些用法中 UIGraphicsImageRenderer 不會返回,具體取決於其閉包包含的內容。 這是一個例子:

    let mainImageRenderer = UIGraphicsImageRenderer(size: theSize)
    let mainImage = mainImageRenderer.image { context in
        context.cgContext.setFillColor(UIColor.clear.cgColor)
        context.fill(CGRect(origin: .zero, size: screenSize))
        let rectangle = CGRect(x: 0, y: 0, width: 50, height: 50)
        context.cgContext.setFillColor(UIColor.red.cgColor)
        context.cgContext.setStrokeColor(UIColor.black.cgColor)
        context.cgContext.setLineWidth(10)
        context.cgContext.addRect(rectangle)
        context.cgContext.drawPath(using: .fillStroke)
        NSLog("Line 1 - This line logged")
    }
    NSLog("Line 2 - This line not logged and no image is created")

如果此代碼在應用程序本身中運行(或在通知內容擴展中運行),那么它可以工作並且到達第 2 行,但是當它在通知服務擴展中運行時,永遠不會到達第 2 行。 例如,如果閉包包括UIImage.draw(at:)UIImage.draw(in) ,則其行為類似。 但是,如果閉包包含 NSAttributedString.draw() 則代碼在從通知擴展中調用時按預期執行,這里的示例代碼確實有效,在這種情況下第 2 行確實到達:

let textImage = textRenderer.image { context in
    context.cgContext.setFillColor(gray.cgColor)
    context.fill(CGRect(origin: .zero, size: textRectangleSize))
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = .center
    let attrs: [NSAttributedString.Key: Any] = [
        .font: UIFont.systemFont(ofSize: 10),
        .paragraphStyle: paragraphStyle
    ]
    let attributedString = NSAttributedString(string: transcript, attributes: attrs)
    attributedString.draw(with: CGRect(x: 10, y: 10, width: textRectangleSize.width - 20, height:textRectangleSize.height - 20),
                          options: .usesLineFragmentOrigin, context: nil)
    NSLog("Line 1 - This line logged")
}
NSLog("Line 2 - Now this line is logged and image is created")

是否有一些根本原因/問題阻止 UIKit 在通知服務擴展中正常運行? 如果是這樣,那么為什么 NSAttributedString.draw 不會導致 UIGraphicsImageRenderer 出現任何問題,但其他諸如 UIImage.draw() 之類的東西會導致問題?

是否有可以用來代替 UIGraphicsImageRenderer() 的替代圖形 API? 我基本上希望能夠在另一個 UIImage 內繪制一個矩形。

更新:在做了更多的實驗之后,我發現正在創建的圖像的大小會影響結果,例如這失敗了:

private class func createBackdropImage() -> UIImage {
    let screenSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
    let renderer = UIGraphicsImageRenderer(size: screenSize)
    let image = renderer.image { context in
        context.cgContext.setFillColor(UIColor.red.cgColor)
        context.fill(CGRect(origin: .zero, size: screenSize))
    }
    NSLog("May or may not reach here depending upon size")
    return image
}

如果我運行以下命令,我的手機上的寬度/高度值將顯示為 375/812,這會導致代碼無法按預期執行,並且無法到達日志行。 但是,如果尺寸變小,那么它會按預期工作。 我嘗試將其從 375/812 降低到 370/800,但仍然失敗,但是如果我將其變小,例如 300/700,它就會開始工作。

為什么正在創建的圖像的大小會產生這種影響? 為什么只有在通知服務擴展中才會發生這種行為?

我認為這可能是 memory 問題。 擴展限制了 memory 並且嘗試對圖像進行圖像處理,屏幕尺寸的大小可能對它來說太大了,因此為什么減小正在創建的圖像的整體大小使其工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM