ios -bitmap上下文生成图片 生成水印

- (void)test
{
    // 0. 加载背景图片
    UIImage *image = [UIImage imageNamed:@"psb"];

    // 1.创建bitmap上下文
    // 执行完这一行在内存中就相遇创建了一个UIImage
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);

    // 2.绘图图片
    // 2.1绘制背景图片
    [image drawAtPoint:CGPointMake(0, 0)];

    // 2.2绘制水印

     UIImage *logImage = [UIImage imageNamed:@"logo1"];

     CGFloat margin = 10;
     CGFloat logY = margin;
     CGFloat logX = image.size.width - margin - logImage.size.width;
     [logImage drawAtPoint:CGPointMake(logX, logY)];


    // 2.3绘制文字水印
    NSString *str = @"黑马程序员";
    [str drawAtPoint:CGPointMake(150, 50) withAttributes:nil];

    // 3.获得bitmap上下文中绘制号的图片
    UIImage *newImage =  UIGraphicsGetImageFromCurrentImageContext();

    // 4.将图片写到文件中
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"aaa.png"];
    NSLog(@"%@", path);

    NSData *data = UIImagePNGRepresentation(newImage);
    [data writeToFile:path atomically:YES];
}

 

posted @ 2018-08-31 15:07  神来钥匙-陈诗友  阅读(751)  评论(0编辑  收藏  举报