簡(jiǎn)介
圖片上下文,圖片上下文的繪制不需要在drawRect:方法中進(jìn)行,在一個(gè)普通的OC方法中就可以繪制。
獲取圖片上下文
使用兩個(gè)方法同樣都可以創(chuàng)建,但是使用第一個(gè)方法將來(lái)創(chuàng)建的圖片清晰度和質(zhì)量沒(méi)有第二種方法的好。
// 參數(shù): 指定將來(lái)創(chuàng)建出來(lái)的bitmap的大小
UIGraphicsBeginImageContext(CGSize size);
/*
* 參數(shù)一: 指定將來(lái)創(chuàng)建出來(lái)的bitmap的大小
* 參數(shù)二: 設(shè)置透明YES代表透明,NO代表不透明
* 參數(shù)三: 代表縮放,0代表不縮放
*/
UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);
使用步驟
- 獲取圖片上下文
- 繪圖
- 從圖片上下文中獲取繪制的圖片
- 關(guān)閉圖片上下文
- (UIImage *)drawImageWithImageNamed:(NSString *)name {
// 獲取圖片
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:nil]];
// 1.開(kāi)啟圖形上下文
UIGraphicsBeginImageContext(image.size);
// 2.繪制到圖形上下文中
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
// 3.從上下文中獲取圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// 4.關(guān)閉圖形上下文
UIGraphicsEndImageContext();
//返回圖片
return newImage;
}
實(shí)際應(yīng)用
圖片添加水印
- 添加文字水印
- (UIImage *)waterImageWithImage:(UIImage *)image text:(NSString *)text textPoint:(CGPoint)point attributedString:(NSDictionary * )attributed {
//1.開(kāi)啟上下文 CGSize size 尺寸, BOOL opaque 透明度, CGFloat scale 比例
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.關(guān)閉圖形上下文
UIGraphicsEndImageContext();
//返回圖片
return newImage;
}
加載圖片
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
// 原始圖片
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"img_meizi.jpg" ofType:nil]];
// 文字屬性
NSDictionary *attributed = @{NSFontAttributeName: [UIFont systemFontOfSize:50.0f], NSForegroundColorAttributeName: [UIColor redColor]};
// 添加水印
imageView.image = [self waterImageWithImage:image text:@"添加一個(gè)水印" textPoint:CGPointMake(50, 50) attributedString:attributed];
[self.view addSubview:imageView];
- 添加圖片水印
- (UIImage *)waterImageWithImage:(UIImage *)image waterImage:(UIImage *)waterImage waterImageRect:(CGRect)rect {
//1.開(kāi)啟上下文
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
//2.繪制背景圖片
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
//3.繪制水印圖片到當(dāng)前上下文
[waterImage drawInRect:rect];
//4.從上下文中獲取新圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//5.關(guān)閉圖形上下文
UIGraphicsEndImageContext();
//返回圖片
return newImage;
}
加載圖片
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
// 原始圖片
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"img_meizi.jpg" ofType:nil]];
// 水印圖片
UIImage *waterImg = [UIImage imageNamed:@"chat_send_nor"];
// 添加水印
imageView.image = [self waterImageWithImage:image waterImage:waterImg waterImageRect:CGRectMake(50, 50, 64, 58)];
[self.view addSubview:imageView];
圖片裁剪
- 裁剪圓形圖片
- (UIImage *)clipCircleImageWithImage:(UIImage *)image circleRect:(CGRect)rect {
//1、開(kāi)啟上下文
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
//2、設(shè)置裁剪區(qū)域
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:rect];
//裁剪
[path addClip];
//3、繪制圖片
[image drawAtPoint:CGPointZero];
//4、獲取新圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//5、關(guān)閉上下文
UIGraphicsEndImageContext();
//6、返回新圖片
return newImage;
}
- 裁剪邊框圖片
- (UIImage *)clipCircleImageWithImage:(UIImage *)image circleRect:(CGRect)rect borderWidth:(CGFloat)borderW borderColor:(UIColor *)borderColor {
//1、開(kāi)啟上下文
UIGraphicsBeginImageContext(image.size);
//2、設(shè)置邊框
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:rect];
//填充邊框
[borderColor setFill];
[path fill];
//3、設(shè)置裁剪區(qū)域
UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(rect.origin.x + borderW , rect.origin.x + borderW , rect.size.width - borderW * 2, rect.size.height - borderW *2)];
[clipPath addClip];
//3、繪制圖片
[image drawAtPoint:CGPointZero];
//4、獲取新圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//5、關(guān)閉上下文
UIGraphicsEndImageContext();
//6、返回新圖片
return newImage;
}
截屏
+ (void)cutScreenWithView:(nullable UIView *)view successBlock:(nullable void(^)(UIImage * _Nullable image,NSData * _Nullable imagedata))block {
//1、開(kāi)啟上下文
UIGraphicsBeginImageContext(view.bounds.size);
//2.獲取當(dāng)前上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//3.截屏
[view.layer renderInContext:ctx];
//4、獲取新圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//5.轉(zhuǎn)化成為Data
//compressionQuality:表示壓縮比 0 - 1的取值范圍
NSData *data = UIImageJPEGRepresentation(newImage, 1);
//6、關(guān)閉上下文
UIGraphicsEndImageContext();
//7.回調(diào)
block(newImage, data);
}
擦除
- (UIImage *)wipeImageWithView:(UIView *)view currentPoint:(CGPoint)point size:(CGSize)size {
//計(jì)算位置
CGFloat offsetX = point.x - size.width * 0.5;
CGFloat offsetY = point.y - size.height * 0.5;
CGRect clipRect = CGRectMake(offsetX, offsetY, size.width, size.height);
//開(kāi)啟上下文
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0);
//獲取當(dāng)前的上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//把圖片繪制上去
[view.layer renderInContext:ctx];
//把這一塊設(shè)置為透明
CGContextClearRect(ctx, clipRect);
//獲取新的圖片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//關(guān)閉上下文
UIGraphicsEndImageContext();
//重新賦值圖片
return newImage;
}