iOS UIImage绘制、压缩、水印、截屏

  • 废话不多说,直接上代码
#import "ImgeController.h"

@interface ImgeController ()

@property (nonatomic, strong) UIImageView *ivCover;

@end

@implementation ImgeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    _ivCover = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 400, 400)];
    _ivCover.contentMode = UIViewContentModeScaleAspectFit;
    _ivCover.backgroundColor = [UIColor redColor];
    
//    _ivCover.image = [self scaleImage:[UIImage imageNamed:@"linshitupian"] sclae:0.2 ratio:0.5];
    
//    _ivCover.image = [self createImageColor:[UIColor orangeColor] size:CGSizeMake(100, 100)];
    
//    _ivCover.image = [self waterAtImage:[UIImage imageNamed:@"linshitupian"] text:@"水印" point:CGPointMake(0, 0) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSBackgroundColorAttributeName:[UIColor purpleColor],NSForegroundColorAttributeName:[UIColor cyanColor]}];
    
    _ivCover.center = self.view.center;
    [self.view addSubview:_ivCover];
    
}

#pragma mark - 截屏操作
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    __weak typeof(self) wSlef = self;
    [self cutView:self.view success:^(UIImage *image) {
        wSlef.ivCover.image = image;
        wSlef.ivCover.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255) / 255.0 green:arc4random_uniform(255) / 255.0 blue:arc4random_uniform(255) / 255.0 alpha:1];
    }];

}

#pragma mark - 截屏
-(void)cutView:(UIView *)view success:(void(^)(UIImage *image))success {
    //开启图形上下文
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0);
    //获取当前上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //渲染
    [view.layer renderInContext:ctx];
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    success(newImage);
}

#pragma mark - 绘制水印
-(UIImage *)waterAtImage:(UIImage *)image
                    text:(NSString *)text
                   point:(CGPoint)point
              attributes:(NSDictionary *)attributes {
    //开启图形上下文
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    //绘制图片
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    //添加文字
    [text drawAtPoint:point withAttributes:attributes];
    //获取图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //关闭上下文
    UIGraphicsEndImageContext();
    return newImage;
}

#pragma mark - 绘制图片
-(UIImage *)createImageColor:(UIColor *)color size:(CGSize)size {
    //开启图形上下文
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    //绘制颜色区域
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, size.width, size.height)];
    [color setFill];
    [path fill];
    //从图形上下文获取图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //关闭图形上下文
    UIGraphicsEndImageContext();
    
    return newImage;
}

#pragma mark - 压缩图片
-(UIImage *)scaleImage:(UIImage *)image sclae:(CGFloat)scale ratio:(CGFloat)ratio{
    //确定压缩后的size
    CGFloat scaleWidth = image.size.width * scale;
    CGFloat scaleHeight = image.size.height * scale;
    CGSize scaleSize = CGSizeMake(scaleWidth, scaleHeight);
    //开启图形上下文
    UIGraphicsBeginImageContext(scaleSize);
    //绘制图片
    [image drawInRect:CGRectMake(0, 0, scaleWidth, scaleHeight)];
    //从图形上下文获取图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //压缩图片
    NSData *d = UIImageJPEGRepresentation(newImage, ratio);
    //重新data转回image
    UIImage *endImage = [UIImage imageWithData:d];
    //关闭图形上下文
    UIGraphicsEndImageContext();
    return endImage;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容