iPhone, how does one overlay one image onto another to create a new image to save? (watermark)

前端 未结 5 1190
逝去的感伤
逝去的感伤 2021-01-30 00:13

Basically I want to take an image that the user chooses from their photo library and then apply a watermark, a triangle in the lower right that has the app name on it. I have th

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-30 01:06

    It's pretty easy:

    UIImage *backgroundImage = [UIImage imageNamed:@"image.png"];
    UIImage *watermarkImage = [UIImage imageNamed:@"watermark.png"];
    
    UIGraphicsBeginImageContext(backgroundImage.size);
    [backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
    [watermarkImage drawInRect:CGRectMake(backgroundImage.size.width - watermarkImage.size.width, backgroundImage.size.height - watermarkImage.size.height, watermarkImage.size.width, watermarkImage.size.height)];
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    If you want the background and watermark to be of the same size then use this code

    ...
    [backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
    [watermarkImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
    ...
    

提交回复
热议问题