一.圖片水印
1.創(chuàng)建個(gè)UIImageView
@property (weak, nonatomic) IBOutlet UIImageView *neImage;
2.創(chuàng)建個(gè)方法實(shí)現(xiàn)水印功能
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *bgImage = [UIImage imageNamed:@""];
//創(chuàng)建一個(gè)位圖上下文
UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0);
//將背景圖片畫入位圖中
[bgImage drawInRect:CGRectMake(0, 0, bgImage.size.width, bgImage.size.height)];
//將水印Logo畫入背景圖中
UIImage *waterIma = [UIImage imageNamed:@""];
[waterIma drawInRect:CGRectMake(bgImage.size.width - 40 - 5, bgImage.size.height - 40 - 5, 40, 40)];
//取得位圖上下文中創(chuàng)建的新的圖片
UIImage *newimage = UIGraphicsGetImageFromCurrentImageContext();
//結(jié)束上下文
UIGraphicsEndImageContext();
//在創(chuàng)建的ImageView上顯示出新圖片
self.neImage.image = newimage;
//壓縮新照片為PNG格式的二進(jìn)制數(shù)據(jù)
NSData *data = UIImagePNGRepresentation(newimage);
//將圖片寫入到手機(jī)存儲(chǔ)中
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"new.png"];
[data writeToFile:path atomically:YES];
}
二.圖片裁剪
//1.加載原圖
UIImage *oldImage = [UIImage imageNamed:@"me"];
//2.獲取位圖上下文
CGFloat bigCic = oldImage.size.width + 2 * 2;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(bigCic, bigCic), NO, 0.0);
//3.畫大圓
[[UIColor whiteColor] set];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddArc(ctx, bigCic * 0.5, bigCic * 0.5, bigCic * 0.5, 0, M_PI * 2, 0);
CGContextFillPath(ctx);
//4.畫小圓
CGFloat smallCic = oldImage.size.width * 0.5;
CGContextAddArc(ctx, bigCic * 0.5 , bigCic * 0.5, smallCic, 0, M_PI * 2, 0);
CGContextClip(ctx);
//5.畫圖
[oldImage drawInRect:CGRectMake(2, 2, oldImage.size.width, oldImage.size.height)];
//6.獲取新圖
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//7.結(jié)束上下文
UIGraphicsEndImageContext();
//8.顯示新圖
self.IconView.image = newImage;
//9.寫入到手機(jī)存儲(chǔ)
NSData *data = UIImagePNGRepresentation(newImage);
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"newClip.png"];
[data writeToFile:path atomically:YES];
三,屏幕截圖
//1.開啟位圖上下文
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0);
//2.渲染截圖
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
//3.獲取新圖
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//4.寫入到手機(jī)存儲(chǔ)
NSData *data = UIImagePNGRepresentation(newImage);
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"newClip.png"];
[data writeToFile:path atomically:YES];
//5.關(guān)閉上下文
UIGraphicsEndImageContext();