天天看點

iOS開發---給圖檔加水印

在UIImage的Categoty中添加的方法

// 給原圖添加圖檔水印

+ (UIImage *)imageWithOriginImg:(UIImage *)originImg WaterMask:(UIImage *)mask inRect:(CGRect)rect

{

    UIGraphicsBeginImageContextWithOptions(originImg.size, NO, 0.0);

    //原圖

    [originImg drawInRect:CGRectMake(0, 0, originImg.size.width, mask.size.height)];

    //水印圖

    [mask drawInRect:rect];

    UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImg;

}

// 給原圖添加文字水印

+ (UIImage *)watermarkImage:(UIImage *)img withName:(NSString *)name

{

    NSString *mark = name;

    int w = img.size.width;

    int h = img.size.height;

    UIGraphicsBeginImageContext(img.size);

    [img drawInRect:CGRectMake( 0, 0, w, h)];

    NSDictionary *attr = @{

                           NSFontAttributeName:[UIFont boldSystemFontOfSize:55.0],

                           NSForegroundColorAttributeName:[UIColor redColor]

                           };

    [mark drawInRect:CGRectMake(30, 20, 100, 50) withAttributes:attr];

    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

    return img;

}