如何在另一个uiimageview上保存uiimageview(swift3) [英] How to save a uiimageview on top of another uiimageview (swift3)
问题描述
导入UIKit
class ViewController:UIViewController {
var image1 = UIImage(named:f.jpg)
var image2 = UIImage(命名为:l.jpg)
覆盖func viewDidLoad(){
super.viewDidLoad()
//在加载视图后执行任何其他设置,通常从笔尖。
$ b}
func saveImage(image1:UIImage,image2:UIImage) - > UIImage {
let size1 = image1.size
let size2 = image2.size
let origin = CGPoint(x:size1.width-size2.width,y :size1.height - size2.height)
UIGraphicsBeginImageContextWithOptions(size2,false,0.0)
image1.draw(in:CGRect(origin:CGPoint.zero,size:size1 ))
image2.draw(in:CGRect(origin:origin,size:size2))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage !
@IBAction func d(_ sender:Any){
UIImageWriteToSavedPhotosAlbum(saveImage(image1:image1 !, image2:image2!),self,#selector(image(_:didFinishSavingWithError: $ _
$ b func image(_ image:UIImage,didFinishSavingWithError error:Error ?, contextInfo:UnsafeRawPointer){
if error = 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 change image has saved to your photos。,preferredStyle :.alert)
ac.addAction(UIAlertAction(title:OK,style:.default))
present(ac,animated:true)
}
}}
您在saveImage函数中的代码应该可以工作 - 假设 image1是然而,如果image1和image2的大小相同,那么您只会看到第二个图像b因为原点将变成(0,0)。换句话说,如果image1和image2都是300x300,那么image2会绘制image1。
IBAction方法看起来是正确连接的,我相信您缺少的是什么是调整第二幅图像的一些逻辑。使用下面的代码来指导你,数学可能会改变,取决于你要找的image1到image2的比例。
func cropImageIntoQuarterSquare(image:UIImage) - > UIImage的? {
let originalImageSize:CGSize = image.size
let smallImageSize = CGSize(width:(originalImageSize.width / 4),height:(originalImageSize.height / 4))
UIGraphicsBeginImageContext(smallImageSize)
image.draw(at:CGPoint.zero)
let imageResult = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return imageResult
}
如果您包含调整大小的代码,那么您的IBAction可能如下所示:
@IBAction func save(_ sender:Any){
guard let croppedImage = cropImageIntoQuarterSquare(image:image2),
let combinedImage = saveImage(image1:image1,image2:croppedImage)else {
//处理错误
return
}
UIImageWriteToSavedPhotosAlbum(combinedImage,self,#selector(image (_:didFinishSavingWithError:contextInfo :)),无)
}
func saveImag e(image1:UIImage,image2:UIImage) - > UIImage的? {
let size1 = image1.size
let size2 = image2.size
let origin = CGPoint(x:size1.width - size2.width,y:size1.height - size2.height)
UIGraphicsBeginImageContext(size1)
image1.draw(in:CGRect(origin:CGPoint.zero,size:size1))
image2.draw(in:CGRect(origin:origin ,size:size2))
let combinedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return combinedImage
}
func image(_ image:UIImage,didFinishSavingWithError错误:错误?, contextInfo:UnsafeRawPointer){
如果让error = 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 change image has saved to your photos。,preferredStyle :.alert)
ac.addAction(UIAlertAction(title:OK,style:.default))
present(ac,animated:true)
}
}
编辑:忘记授予@Peter Tao一些他已经提供的代码 - 以及从hackingwithswift.com他提供。
Right Now I have two image views. All the button does is save the photo with the red box over the blue box and it will save image2 in the blue box and image1 in the red box. I would like red box to be saved over the blue box in just the small bottom right section of the blue box. Just like a lower third you see on tv ]]
import UIKit
class ViewController: UIViewController {
var image1 = UIImage(named: "f.jpg")
var image2 = UIImage(named: "l.jpg")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func saveImage(image1:UIImage, image2:UIImage) -> UIImage {
let size1 = image1.size
let size2 = image2.size
let origin = CGPoint(x: size1.width-size2.width, y: size1.height - size2.height)
UIGraphicsBeginImageContextWithOptions(size2, false, 0.0)
image1.draw(in: CGRect(origin: CGPoint.zero, size: size1))
image2.draw(in: CGRect(origin: origin, size: size2))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
@IBAction func d(_ sender: Any) {
UIImageWriteToSavedPhotosAlbum(saveImage(image1:image1!, image2:image2!), 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)
}
}}
The code you have in saveImage function should work - assuming that image1 is larger than image2, however, if image1 and image2 are the same sizes then you will only see the second image because the origin will become (0,0). So in other words, if image1 and image2 are both 300x300, then image2 draws over image1.
The IBAction method appears to be wired up correctly and I believe what you are missing is some logic to resize the second image. Use the code below to guide you, the math will likely change depending on what ratio of image1 to image2 you're looking for.
func cropImageIntoQuarterSquare(image: UIImage) -> UIImage? {
let originalImageSize: CGSize = image.size
let smallImageSize = CGSize(width: (originalImageSize.width / 4), height: (originalImageSize.height / 4))
UIGraphicsBeginImageContext(smallImageSize)
image.draw(at: CGPoint.zero)
let imageResult = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return imageResult
}
If you incorporate the resizing code then your IBAction might look something like this:
@IBAction func save(_ sender: Any) {
guard let croppedImage = cropImageIntoQuarterSquare(image: image2),
let combinedImage = saveImage(image1: image1, image2: croppedImage) else {
// handle the error
return
}
UIImageWriteToSavedPhotosAlbum(combinedImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
func saveImage(image1: UIImage, image2: UIImage) -> UIImage? {
let size1 = image1.size
let size2 = image2.size
let origin = CGPoint(x: size1.width - size2.width, y: size1.height - size2.height)
UIGraphicsBeginImageContext(size1)
image1.draw(in: CGRect(origin: CGPoint.zero, size: size1))
image2.draw(in: CGRect(origin: origin, size: size2))
let combinedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return combinedImage
}
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)
}
}
Edit: Forgot to give credit to @Peter Tao for some of the code he already provided - and the link from hackingwithswift.com he provided.
这篇关于如何在另一个uiimageview上保存uiimageview(swift3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!