iOS 图片合成(一)
生活随笔
收集整理的這篇文章主要介紹了
iOS 图片合成(一)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
需求:我們要將非固定的文本、圖片等信息合成到一張圖片上,例如我們要做一個分享的功能,有一張底圖,在這底圖上我們要把用戶的頭像和昵稱放上去。這里我們用到Quart2D繪圖之UIGraphicsBeginImageContextWithOptions 圖形上下文的功能。
1、在指定圖片上添加另一張圖片(被添加者我們稱之為水印)
+ (UIImage *)pq_WaterImageWithImage:(UIImage *)image waterImage:(UIImage *)waterImage waterImageRect:(CGRect)rect{//1.獲取圖片//2.開啟上下文UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);//3.繪制背景圖片[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];//繪制水印圖片到當前上下文[waterImage drawInRect:rect];//4.從上下文中獲取新圖片UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();//5.關閉圖形上下文UIGraphicsEndImageContext();//返回圖片return newImage; }注意這里的rect需要輸入像素坐標
2、把指定文本合成到圖片上
+ (UIImage *)pq_WaterImageWithImage:(UIImage *)image text:(NSString *)text textPoint:(CGPoint)point attributedString:(NSDictionary * )attributed{//1.開啟上下文UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);//2.繪制圖片[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];//添加水印文字[text drawAtPoint:point withAttributes:attributed];//3.從上下文中獲取新圖片UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();//4.關閉圖形上下文UIGraphicsEndImageContext();//返回圖片return newImage;}3、剪切指定圖片
+ (nullable UIImage *)pq_ClipCircleImageWithImage:(nullable UIImage *)image circleRect:(CGRect)rect{//1、開啟上下文UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);//2、設置裁剪區域UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:rect];[path addClip];//3、繪制圖片[image drawAtPoint:CGPointZero];//4、獲取新圖片UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();//5、關閉上下文UIGraphicsEndImageContext();//6、返回新圖片return newImage; }具體的使用請參照我下一篇博客
我是磊懷 2849765859 是我的QQ,聯系我。
總結
以上是生活随笔為你收集整理的iOS 图片合成(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言实现hdr图像合成,怎样完成一张H
- 下一篇: 未来宝宝照片合成,怎么做到的呢?