基础知识拾遗iOS开发常用iOS开发

UIGraphicsBeginImageContextWithO

2018-02-07  本文已影响1457人  Carson_Zhu

简介

图片上下文,图片上下文的绘制不需要在drawRect:方法中进行,在一个普通的OC方法中就可以绘制。

获取图片上下文

使用两个方法同样都可以创建,但是使用第一个方法将来创建的图片清晰度和质量没有第二种方法的好。

// 参数: 指定将来创建出来的bitmap的大小
UIGraphicsBeginImageContext(CGSize size);
/*
 * 参数一: 指定将来创建出来的bitmap的大小
 * 参数二: 设置透明YES代表透明,NO代表不透明
 * 参数三: 代表缩放,0代表不缩放
 */
UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);
使用步骤
- (UIImage *)drawImageWithImageNamed:(NSString *)name {
    // 获取图片
    UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:nil]];
    // 1.开启图形上下文
    UIGraphicsBeginImageContext(image.size);
    // 2.绘制到图形上下文中
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    // 3.从上下文中获取图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    // 4.关闭图形上下文
    UIGraphicsEndImageContext();
    //返回图片
    return newImage;
}

实际应用

图片添加水印
- (UIImage *)waterImageWithImage:(UIImage *)image text:(NSString *)text textPoint:(CGPoint)point attributedString:(NSDictionary * )attributed {
    //1.开启上下文 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.关闭图形上下文
    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:@"添加一个水印" textPoint:CGPointMake(50, 50) attributedString:attributed];
[self.view addSubview:imageView];
- (UIImage *)waterImageWithImage:(UIImage *)image waterImage:(UIImage *)waterImage waterImageRect:(CGRect)rect {
    //1.开启上下文
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    //2.绘制背景图片
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    //3.绘制水印图片到当前上下文
    [waterImage drawInRect:rect];
    //4.从上下文中获取新图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //5.关闭图形上下文
    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、开启上下文
    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;
}
- (UIImage *)clipCircleImageWithImage:(UIImage *)image circleRect:(CGRect)rect borderWidth:(CGFloat)borderW borderColor:(UIColor *)borderColor {
    //1、开启上下文
    UIGraphicsBeginImageContext(image.size);
    //2、设置边框
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:rect];
    //填充边框
    [borderColor setFill];
    [path fill];
    //3、设置裁剪区域
    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、关闭上下文
    UIGraphicsEndImageContext();
    //6、返回新图片
    return newImage;
}
截屏
+ (void)cutScreenWithView:(nullable UIView *)view successBlock:(nullable void(^)(UIImage * _Nullable image,NSData * _Nullable imagedata))block {
    //1、开启上下文
    UIGraphicsBeginImageContext(view.bounds.size);
    //2.获取当前上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //3.截屏
    [view.layer renderInContext:ctx];
    //4、获取新图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //5.转化成为Data
    //compressionQuality:表示压缩比 0 - 1的取值范围
    NSData *data = UIImageJPEGRepresentation(newImage, 1);
    //6、关闭上下文
    UIGraphicsEndImageContext();
    //7.回调
    block(newImage, data);
}
擦除
- (UIImage *)wipeImageWithView:(UIView *)view currentPoint:(CGPoint)point size:(CGSize)size {
    //计算位置
    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);
    //开启上下文
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0);
    //获取当前的上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //把图片绘制上去
    [view.layer renderInContext:ctx];
    //把这一块设置为透明
    CGContextClearRect(ctx, clipRect);
    //获取新的图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //关闭上下文
    UIGraphicsEndImageContext();
    //重新赋值图片
    return newImage;
}
上一篇下一篇

猜你喜欢

热点阅读