My code right now just saves 1 image. I would like to save a smaller image in the top left corner above the first image. The big thing is just to make it save together in the photo gallery. 2 images on top of each other.
import UIKit
class ViewController: UIViewController {
let imageView = UIImageView(frame: CGRect(x: 50, y: 50, width: 300, height: 300))
let imageView2 = UIImageView(frame: CGRect(x:200, y: 50, width: 100, height: 100))
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let image = UIImage(named: "wall")!
imageView.image = image
let image2 = UIImage(named: "pic")!
imageView2.image = image2
imageView.addSubview(imageView2)
imageView2.image = image2
imageView.addSubview(imageView2)
let topImage = UIImage(named: "wall")
let bottomImage = UIImage(named: "pic")
let size = CGSize(width: topImage!.size.width, height: topImage!.size.height + bottomImage!.size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
topImage!.draw(in: CGRect(x: 0, y: topImage!.size.height, width: size.width, height: bottomImage!.size.height))
bottomImage!.draw(in: CGRect(x: 0, y: topImage!.size.height, width: size.width, height: bottomImage!.size.height))
//your new Image
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
}
@IBAction func press(_ sender: Any) {
UIImageWriteToSavedPhotosAlbum(imageView.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
if let error = error {
// we got back an error!
let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
} else {
let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}}}
let imageView2 = UIImageView(frame: CGRect(x:200, y: 50, width: 100, height:100))
let image2 = UIImage(named: "wallsmallImage")!
imageView2.image = image2
imageView.addSub(imageView2)
#Now merge both images
let image = UIImage(named: "wall")!
imageView.image = image
let image2 = UIImage(named: "pic")!
imageView2.image = image2
imageView.addSubview(imageView2)
let topImage = UIImage(named: "wall")
let bottomImage = UIImage(named: "pic")
let size = CGSize(width: topImage!.size.width, height: topImage!.size.height + bottomImage!.size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
topImage!.draw(in: CGRect(x: 0, y: topImage!.size.height, width: size.width, height: bottomImage!.size.height))
bottomImage!.draw(in: CGRect(x: 0, y: topImage!.size.height, width: size.width, height: bottomImage!.size.height))
//your new Image
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
All of this code goes into a different function to add it to a image view.
let bottomImage:UIImage = UIImage(named: "backdrop")!
let topImage:UIImage = UIImage(named:"wall")!
let topImage2:UIImage = UIImage(named:"wall")!
let tpp:UIImage = letImageVIEW.image!
let right:UIImage = rightIMAGEVIEW.image!
// Change here the new image size if you want
let newSize = CGSize(width: bottomImage.size.width, height: bottomImage.size.height )
let newSize2 = CGSize(width: bottomImage.size.width, height: bottomImage.size.height)
UIGraphicsBeginImageContextWithOptions(newSize, false, bottomImage.scale)
// bottomImage.draw(in: CGRect(x: 0,y: 0,width: newSize.width,height: newSize.height))
tpp.draw(in: CGRect(x: 0,y: 0,width: newSize2.width,height: newSize2.height), blendMode:CGBlendMode.normal, alpha:1.0)
right.draw(in: CGRect(x: 0,y: 0,width: newSize2.width,height: newSize2.height), blendMode:CGBlendMode.normal, alpha:0.2)
topImage.draw(in: CGRect(x: 0,y: 0,width: newSize.width/2,height: newSize.height/2), blendMode:CGBlendMode.normal, alpha:1.0)
topImage2.draw(in: CGRect(x: 50,y: 0,width: newSize2.width/4,height: newSize2.height/4), blendMode:CGBlendMode.normal, alpha:1.0)
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
finalImage.image = newImage
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.