簡體   English   中英

如何在圖像上繪制文字

[英]How to draw text on image

我有一張圖片。 在該圖像中,我需要繪制文本。

請提供任何鏈接或示例代碼進行嘗試。

謝謝你的幫助

只需合成一個額外的視圖,即可在圖像頂部包含文本。 如果將UILabel與[UIColor clearColor]使用作為背景色,並將opaque設置為false,則只會在圖像頂部看到文本,而不會遮擋任何圖像。

如果要從具有透明視圖(在其頂部包含文本)的視圖中從圖像中獲取圖像,請參見以下答案:

如何從UIView的內容獲取CGImageRef?

像這樣:

    UIView* baseView = [[UIView alloc] initWithFrame:frameSize];

    UIImage* imageToCaption = [UIImage imageNamed:@"myImage.jpg"];
    UIImageView* imageToCaptionView = [[UIImageView alloc] initWithImage:imageToCaption];
    imageToCaptionView.frame = frameSize;
    [baseView addSubview:imageToCaptionView];
    [imageToCaptionView release];


    // do stuff with fonts to calculate the size of the label, center it in your image etc. etc.
    CGRect labelFrame = ...

    UILabel* myLabel = [[UILabel alloc] initWithFrame:labelFrame];
    myLabel.backgroundColor = [UIColor clearColor];
    myLabel.opaque = NO;

    // set the color, font, text etc.

    [baseView addSubView:myLabel];
    [myLabel release];

沒有編譯或完成,但是您知道了。 如果要以圖像形式獲取結果,請使用[baseView.layer renderInContext:context];

暫無
暫無

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

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