Saving an image on top of another image in Swift

前端 未结 4 1879
终归单人心
终归单人心 2020-12-09 11:24

I am learning Swift and I am creating an app that uses a personal photo and puts another on top of it. I now have a hacky solution, to create a screenshot of the area and sa

4条回答
  •  没有蜡笔的小新
    2020-12-09 12:05

    Updated to Swift 3.0:

    func saveImage() {
        let bottomImage = UIImage(named: "bottom")!
        let topImage = UIImage(named: "top")!
    
        let newSize = CGSizeMake(100, 100) // set this to what you need
        UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
    
        bottomImage.draw(in: CGRect(origin: CGPointZero, size: newSize))//As drawInRect is deprecated
        topImage.draw(at: CGRect(origin: CGPointZero, size: newSize))//As drawInRect is deprecated
    
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }
    

提交回复
热议问题