簡體   English   中英

將UIImage與UILabel合並為一個UIImage,Swift

[英]Merge UIImage with UILabel into one UIImage, Swift

我試圖將具有UILabel的UIImage合並到一個UIImage中,因為我寫了一個函數,所有工作都很好,除了Label沒有添加到當前圖形上下文中。 多謝您的協助!

    func textToImage(drawText: String, inImage: UIImage, atPoint: CGPoint) -> UIImage{

    // Setup the image context using the passed image
    UIGraphicsBeginImageContext(inImage.size)

    // Put the image into a rectangle as large as the original image

    inImage.draw(in: CGRect(origin: CGPoint.zero, size: CGSize(width: inImage.size.width, height: inImage.size.height)))

    // Create a point within the space that is as bit as the image
    let rectPos = CGPoint(x: atPoint.x, y: atPoint.y)
    let rectSize = CGSize(width: inImage.size.width, height: inImage.size.height)
    let rect = CGRect(origin: rectPos, size: rectSize)

    // Draw the text into an image
    let label = UILabel()
    label.text = drawText
    label.textColor = .white
    label.font = UIFont(name: "Helvetica Bold", size: 12)!

    label.drawText(in: rect)
    // Create a new image out of the images we have created
    let newImage = UIGraphicsGetImageFromCurrentImageContext()

    // End the context now that we have the image we need
    UIGraphicsEndImageContext()

    //Pass the image back up to the caller
    return newImage!
}

您的代碼存在問題,就是UIKit(UILabel用法)試圖在其自己的上下文中繪制內容。 您需要在相同的上下文中繪制文本和圖像才能獲得所需的結果。

嘗試此操作以接收生成的圖像:

func textToImage(drawText text: NSString, inImage image: UIImage) -> UIImage
{

    UIGraphicsBeginImageContext(image.size)
    image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))

    let font=UIFont(name: "Helvetica-Bold", size: 8)!

    let paraStyle=NSMutableParagraphStyle()
    paraStyle.alignment=NSTextAlignment.center

    let attributes = [NSAttributedStringKey.foregroundColor:UIColor.red, NSAttributedStringKey.font:font, NSAttributedStringKey.paragraphStyle:paraStyle]

    let height = font.lineHeight

    let y = (image.size.height-height) / 2

    let strRect = CGRect(x: 0, y: y, width: image.size.width, height: height)

    text.draw(in: strRect.integral, withAttributes: attributes)

    let result=UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return result!
}

@IBAction func touchTest(_ sender: Any)
{
    let button = sender as! UIButton
    let image = self.textToImage(drawText: "my text", inImage: UIImage.init(named: "circle")!)
    button.setBackgroundImage(image, for: UIControlState.normal)
}

您確定要在正確位置繪制標簽嗎? rect是您計算為CGRect(origin: CGPoint.zero, size: CGSize(width: inImage.size.width, height: inImage.size.height))包含atPoint嗎?

問題似乎在這里UIGraphicsBeginImageContext(inImage.size)您正在創建的上下文是圖像的大小。 您需要創建上下文以Image +標簽高度的大小。 然后,您可以相應地添加標簽,並且將能夠看到附加圖像內的標簽。

還指定標簽的框架, let label = UILabel()

假設您要在圖像底部添加標簽。 假設圖像尺寸為300 * 400,並且您想將標簽放在底部。

概念是創建一個高度為400 +標簽高度(假設為40)的上下文。 然后,將標簽框架設置為CGRect(0,imagesize.height,100,40) ay。

希望能幫助到你

好的,我知道了:圖像的分辨率很高,因此文本太小而看不到。

暫無
暫無

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

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