How can I add a watermark to an image using this code?

前端 未结 2 1930
伪装坚强ぢ
伪装坚强ぢ 2020-12-29 16:44

I know there are several other ways to do this; I don\'t want to import anything that I don\'t need to. If someone can help me with his code, that would be great.

Cu

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-29 17:11

    If you grab the UIImageViews' images you could use the following concept:

    if let img = UIImage(named: "image.png"), img2 = UIImage(named: "watermark.png") {
    
        let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)
    
        UIGraphicsBeginImageContextWithOptions(img.size, true, 0)
        let context = UIGraphicsGetCurrentContext()
    
        CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
        CGContextFillRect(context, rect)
    
        img.drawInRect(rect, blendMode: .Normal, alpha: 1)
        img2.drawInRect(CGRectMake(x,y,width,height), blendMode: .Normal, alpha: 1)
    
        let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    
        UIImageWriteToSavedPhotosAlbum(result, nil, nil, nil)
    
    }
    

提交回复
热议问题