iOS學習,ios開發

來源:互聯網
上載者:User

iOS學習,ios開發
繪製到位元影像

下面利用位元影像圖形上下文給一個圖片添加浮水印,在下面的程式中我們首先建立上下文,然後在上下文中繪製圖片、直線和文本,最後從當前位元影像上下文中取得最終形成的新圖片顯示到介面

- (void)viewDidLoad {    [super viewDidLoad];        UIImage *image = [self drawImageAtImageContext];    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];    imageView.center = CGPointMake(160, 282);    [self.view addSubview:imageView];    }//添加浮水印-(UIImage *)drawImageAtImageContext{    //獲得一個位元影像圖形上下文    CGSize size = CGSizeMake(300, 188);//畫布大小        UIGraphicsBeginImageContext(size);        UIImage *image = [UIImage imageNamed:@"frame_shop_lovely_bg@2x.jpg"];    //注意繪圖的位置是相對於畫布頂點而言,不是螢幕    [image drawInRect:CGRectMake(0, 0, 300, 188)];    //添加浮水印    CGContextRef ref = UIGraphicsGetCurrentContext();    //字元的長度和 font    UIFont *font = [UIFont systemFontOfSize:15];    NSString *str = @"大金毛";    int strLength = str.length *font.pointSize;    //兩點確定一條直線    //底線的長度等於字元長度    CGContextMoveToPoint(ref, 200, 178);    CGContextAddLineToPoint(ref, strLength + 200, 178);    //直線的顏色、寬度    [[UIColor redColor]setStroke];    CGContextSetLineWidth(ref, 2);    //繪製映像到指定圖形上下文,只有邊框    CGContextDrawPath(ref, kCGPathStroke);    //字元的位置    [str drawInRect:CGRectMake(200, 158, 100, 30) withAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:[UIColor redColor]}];    //返回繪製的新圖形    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    //最後一定不要忘了關閉上下文    UIGraphicsEndImageContext();        return newImage;}

來自KenshinCui

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.