iOS:给图片打上水印

说明:

1.加载背景图片,生成bitmap;

2.加载水印图片;

3.合成2张图片,生成新的UIImage。

 

使用:

/*
 合成两张图片,打上水印
 */
- (void)createWatermarkImg{
    //1.0加载背景图片
    UIImage *bg = [UIImage imageNamed:@"bg"];
    //1.1产生背景bitmap
    UIGraphicsBeginImageContextWithOptions(bg.size, NO, 0.0);
    //1.2绘制背景图片
    [bg drawInRect:CGRectMake(0, 0, bg.size.width, bg.size.height)];
    
    //2.0加载logo图标
    UIImage *logo = [UIImage imageNamed:@"logo"];
    //2.1绘制logo
    [logo drawInRect:CGRectMake(bg.size.width - 50, bg.size.height - 50, 50, 50)];
    
    //3.合成新的图片
    UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext();
    
    [self.imgIv setImage:newImg];
    
    //4.结束上下文
    UIGraphicsEndImageContext();
    
    //5.将新的图片压缩成NSData
    NSData *newImgData = UIImagePNGRepresentation(newImg);
    
    //6.写入文件
    //创建新图片路径
    NSString *newImgPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"newimg.png"];
    //将新图片写入文件
    [newImgData writeToFile:newImgPath atomically:YES];
}

 

上一篇:java


下一篇:CodeForces 91B Queue