iOS屏幕截图,裁切图片(制作头像),添加图片水印(Quarz2D)

来源:互联网 发布:淘宝怎么退货怎么邮寄 编辑:程序博客网 时间:2024/05/17 13:43

屏幕截图

//扩展分类使用@implementation UIImage (Clip)+ (instancetype)captureWithView:(UIView *)view{    // 1.开启上下文    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);        // 2.将控制器view的layer渲染到上下文(放什么进去则截图什么)    [view.layer renderInContext:UIGraphicsGetCurrentContext()];        // 3.取出图片    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();        // 4.结束上下文    UIGraphicsEndImageContext();        return newImage;}@end

- (void)clip {        //延迟截图,让按钮不是按下状态就被截图了          ​dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{        // 1.捕捉(调用下面的方法进行 捕抓生成新的image后再存入文件中)        UIImage *newImage = [UIImage captureWithView:self.view];                // 2.写文件        NSData *data = UIImagePNGRepresentation(newImage);        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"new.png"];        [data writeToFile:path atomically:YES];    });}


图片裁剪(可用来制图特定形状的头像等)

+ (instancetype)circleImageWithName:(NSString *)name borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor{    // 1.加载原图    UIImage *oldImage = [UIImage imageNamed:name];        // 2.开启上下文    CGFloat imageW = oldImage.size.width + 2 * borderWidth;    CGFloat imageH = oldImage.size.height + 2 * borderWidth;    CGSize imageSize = CGSizeMake(imageW, imageH);    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);        // 3.取得当前的上下文(上面创建的)    CGContextRef ctx = UIGraphicsGetCurrentContext();        // 4.画边框(大圆)    [borderColor set];    CGFloat bigRadius = imageW * 0.5; // 大圆半径    CGFloat centerX = bigRadius; // 圆心    CGFloat centerY = bigRadius;    CGContextAddArc(ctx, centerX, centerY, bigRadius, 0, M_PI * 2, 0);    CGContextFillPath(ctx); // 画圆        // 5.小圆    CGFloat smallRadius = bigRadius - borderWidth;    CGContextAddArc(ctx, centerX, centerY, smallRadius, 0, M_PI * 2, 0);    // 裁剪(后面画的东西才会受裁剪的影响)    CGContextClip(ctx);        // 6.画图    [oldImage drawInRect:CGRectMake(borderWidth, borderWidth, oldImage.size.width, oldImage.size.height)];        // 7.取图    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();        // 8.结束上下文    UIGraphicsEndImageContext();        return newImage;}

添加图片水印


- (void)testWater{    UIImage *bgImage = [UIImage imageNamed:@"scene"];        // 上小文 : 基于位图(bitmap) ,  所有的东西需要绘制到一张新的图片上去        // 1.创建一个基于位图的上下文(开启一个基于位图的上下文)    // size : 新图片的尺寸    // opaque : YES : 不透明,  NO : 透明    // 这行代码过后.就相当于常见一张新的bitmap,也就是新的UIImage对象    UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0);        // 2.画背景    [bgImage drawInRect:CGRectMake(0, 0, bgImage.size.width, bgImage.size.height)];        // 3.画右下角的水印    UIImage *waterImage = [UIImage imageNamed:@"logo"];    CGFloat scale = 0.2;    CGFloat margin = 5;    CGFloat waterW = waterImage.size.width * scale;    CGFloat waterH = waterImage.size.height * scale;    CGFloat waterX = bgImage.size.width - waterW - margin;    CGFloat waterY = bgImage.size.height - waterH - margin;    [waterImage drawInRect:CGRectMake(waterX, waterY, waterW, waterH)];        // 4.从上下文中取得制作完毕的UIImage对象    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();        // 5.结束上下文    UIGraphicsEndImageContext();        // 6.显示到UIImageView    self.iconView.image = newImage;        // 7.将image对象压缩为PNG格式的二进制数据    NSData *data = UIImagePNGRepresentation(newImage);    //    UIImageJPEGRepresentation(<#UIImage *image#>, <#CGFloat compressionQuality#>)        // 8.写入文件 path 拼接 stringByAppendingPathComponent "/"+@"new.png"    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"new.png"];    [data writeToFile:path atomically:YES];}





0 0
原创粉丝点击