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

前端 未结 2 1751
伪装坚强ぢ
伪装坚强ぢ 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)
    
    }
    
    0 讨论(0)
  • 2020-12-29 17:14

    SWIFT 4 Use this

    let backgroundImage = imageData!
    let watermarkImage = #imageLiteral(resourceName: "jodi_url_icon")
    
    let size = backgroundImage.size
    let scale = backgroundImage.scale
    
    UIGraphicsBeginImageContextWithOptions(size, false, scale)
    backgroundImage.draw(in: CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height))
    watermarkImage.draw(in: CGRect(x: 10, y: 10, width: size.width, height: size.height - 40))
    
    let result = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    

    Use result to UIImageView, tested.

    0 讨论(0)
提交回复
热议问题