【发布时间】:2017-06-05 23:35:47
【问题描述】:
我的代码现在只保存 1 张图片。我想在第一张图片上方的左上角保存一个较小的图片。最重要的是让它一起保存在照片库中。 2 张图像相互重叠。
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)
}}}
【问题讨论】:
标签: ios swift3 imageview save photo