图片水印和裁剪

  • 效果图:
图1

图2

图3
  • 主要代码

// UIImage+image.h

#import <UIKit/UIKit.h>

@interface UIImage (image)
/**
 *  返回一个带有边框的圆形裁剪图片
 *
 *  @param borderW    边框宽度
 *  @param boderColor 边框颜色
 *  @param oriImage   要裁剪的图片
 *
 *  @return 已经裁剪好的带有边框的图片
 */
+ (UIImage *)imageWithBorder:(CGFloat)borderW color:(UIColor *)boderColor image:(UIImage *)oriImage;

/**
 *  返回一个已经添加好文字的图片
 *
 *  @param oriString    要添加的文字
 *  @param oriImage   要裁剪的图片
 *
 *  @return 已经添加好文字的图片
 */
+ (UIImage *)imageWithString:(NSString *)oriString image:(UIImage *)oriImage;

/**
 *  返回一个已经裁剪好的图片

 *  @param oriImage   要裁剪的图片
 *
 *  @return 已经裁剪好的图片
 */
+ (UIImage *)imageWithOriImage:(UIImage *)oriImage;

@end



// UIImage+image.m


#import "UIImage+image.h"

@implementation UIImage (image)

+ (UIImage *)imageWithBorder:(CGFloat)borderW color:(UIColor *)boderColor image:(UIImage *)oriImage {


    //1.确定边框的宽度
    //CGFloat borderW = 10;
    //2.加载图片
    //UIImage *oriImage = [UIImage imageNamed:@"阿狸头像"];
    //3.开启位图上下文(大小 原始图片的宽高度+ 2 *边框宽度)
    CGSize size = CGSizeMake(oriImage.size.width + 2 * borderW, oriImage.size.height + 2 * borderW);
    UIGraphicsBeginImageContext(size);
    //4.绘制边框(大圆)
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)];
    [boderColor set];
    [path fill];
    //5.绘制小圆(把小圆设置成裁剪区域)
    UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderW, borderW, oriImage.size.width, oriImage.size.height)];
    [clipPath addClip];
    //6.把图片绘制到上下文当中
    [oriImage drawAtPoint:CGPointMake(borderW, borderW)];
    //7.从上下文当中生成图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //8.关闭上下文.
    UIGraphicsEndImageContext();

    return newImage;

}

+ (UIImage *)imageWithString:(NSString *)oriString image:(UIImage *)oriImage {
    // 生成一张图片
    // 1.创建位图上下文(size:开启多大的上下文,就会生成多大的图片)
    UIGraphicsBeginImageContext(oriImage.size);
    // 2.把图片绘制到上下文当中
    [oriImage drawAtPoint:CGPointZero];
    // 3.绘制水印
    NSString *str = oriString;


    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    dict[NSForegroundColorAttributeName] = [UIColor redColor];

    [str drawAtPoint:CGPointZero withAttributes:dict];
    //4.从上下文当中生成一张图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //5.关闭位图上下文
    UIGraphicsEndImageContext();

    return newImage;
}

+ (UIImage *)imageWithOriImage:(UIImage *)oriImage {
    //生成一张圆形图片
    //1.开启一个位图上下文
    UIGraphicsBeginImageContext(oriImage.size);
    //2.设置一个裁剪区域(圆形)
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, oriImage.size.width, oriImage.size.height)];
    //把路径设置成裁剪区域(超过裁剪区域以外的内容会自动被裁剪掉)
    [path addClip];

    //3.把图片绘制到上下文当中
    [oriImage drawAtPoint:CGPointZero];
    //4.从上下文当中生成一张图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //5.关闭上下文
    UIGraphicsEndImageContext();

    return newImage;
}

@end


// ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

// ViewController.m
#import "ViewController.h"
#import "UIImage+image.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 已经裁剪好的图片
//    self.imageV.image = [UIImage imageWithOriImage:[UIImage imageNamed:@"阿狸头像"]];
    // 已经裁剪好的带有边框的图片
//    self.imageV.image = [UIImage imageWithBorder:10 color:[UIColor greenColor] image:[UIImage imageNamed:@"阿狸头像"]];
    // 图片加文字
    self.imageV.image = [UIImage imageWithString:@"思行先生" image:[UIImage imageNamed:@"小黄人"]];

}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 图片水印 往图片上加文字 图片裁剪 裁剪圆形图片并在圆形周围加一个宽度为5的蓝色圆环
    一亩三分甜阅读 634评论 0 1
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,387评论 25 708
  • 五一开始,好像一直很忙碌。工作、玩、吃饭喝酒。 听歌较之前多了很多,不知道是不是因为搬家之后骑车上班的缘故。 读书...
    马乂阅读 169评论 0 0
  • 大学的时候才接触网络,上一辈人总说我们是最好的时代,有更多的机会成就不平凡。接触网络之前,感觉自己还不错,接触之后...
    手执桔梗阅读 346评论 2 0
  • 阿北是来自北方的姑娘,我那时候是不喜欢阿北的,她张扬,倔强,浓烈得就像太阳,整张脸就写着,你要是惹我,我就跟你急。...
    江三友阅读 465评论 2 1