圖片水印
往圖片上加文字
UIImage *oldImage = [UIImage imageNamed:@"1.jpg"];
//上下文
UIGraphicsBeginImageContextWithOptions(oldImage.size, NO, 0.0);
//從零點(diǎn)開(kāi)始畫(huà)圖
[oldImage drawAtPoint:CGPointZero];
//添加的文字
NSString *text = @"老板,我要加薪!";
//設(shè)定字體大小和顏色
NSDictionary *dict = @{NSFontAttributeName:[UIFont systemFontOfSize:38],NSForegroundColorAttributeName:[UIColor redColor]};
//設(shè)定要添加字體的位置
[text drawAtPoint:CGPointMake(400, 533) withAttributes:dict];
//得到新的圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//關(guān)閉上下文
UIGraphicsEndImageContext();
_headerImageView.image = newImage;
//將生成的新的圖片轉(zhuǎn)化為data并
NSData *data = UIImagePNGRepresentation(newImage);
//寫(xiě)到桌面
[data writeToFile:@"/Users/aa/Desktop/Image.jpg" atomically:YES];
圖片裁剪
裁剪圓形圖片并在圓形周圍加一個(gè)寬度為5的藍(lán)色圓環(huán)
UIImage *oldImage = [UIImage imageNamed:@"4"];
//圓環(huán)的寬度
CGFloat borderW = 5;
//大圖片的寬度和高度
CGFloat imageW = oldImage.size.width + 2*borderW;
CGFloat imageH = oldImage.size.height + 2*borderW;
//防止有鋸齒現(xiàn)象
CGFloat circirW = imageW > imageH ? imageH : imageW;
//開(kāi)啟上下文
UIGraphicsBeginImageContextWithOptions(CGSizeMake(circirW, circirW), NO, 0.0);
//畫(huà)個(gè)大的圓
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0, circirW, circirW)];
//獲取當(dāng)前上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//添加到上下文
CGContextAddPath(ctx, path.CGPath);
//渲染藍(lán)色
[[UIColor blueColor] set];
//填充
CGContextFillPath(ctx);
//小圓的正切矩形
CGRect clipR = CGRectMake(borderW, borderW, oldImage.size.width, oldImage.size.height);
//畫(huà)小圓
UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:clipR];
//設(shè)置裁剪區(qū)域
[clipPath addClip];
//畫(huà)圖片
[oldImage drawAtPoint:CGPointMake(borderW, borderW)];
//獲取到新的圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//關(guān)閉上下文
UIGraphicsEndImageContext();
_headerImage.image = newImage;
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者