如何在 xcode 中的图像上叠加图像?
how to overlay an image over an image in xcode?
我正在尝试制作一个应用程序,用户可以在其中 select 从他们的图片库中选择一张图片作为背景,然后是他们的徽标,他们也可以从他们的(照片)库中选择...
目前我还没有找到可以帮助我的教程,主要是因为用户必须能够设置自己的图像而不是默认图像。
另外,你们中的任何人也知道我如何拥有多个 UIImagePickerControllers 因为这段代码同时影响两个图像而不是每个选择器一个?
我use/have :
我使用 swift
我使用 xcode 7.0.1
这是
the storyboard i currently use.
我的 swift 文件:
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var logo: UIImageView!
@IBOutlet weak var bgimg: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func selectbg(sender: AnyObject) {
let bgPicker = UIImagePickerController()
bgPicker.delegate = self
bgPicker.sourceType = .PhotoLibrary
self.presentViewController(bgPicker, animated: true, completion: nil)
}
@IBAction func selectlogo(sender: AnyObject) {
let logoPicker = UIImagePickerController()
logoPicker.delegate = self
logoPicker.sourceType = .PhotoLibrary
self.presentViewController(logoPicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
logo.image = image
bgimg.image = image
self.dismissViewControllerAnimated(false, completion: nil)
}
@IBAction func addImage(sender: AnyObject) {
let ActionAlert = UIAlertController(title: nil, message: "What image do you want to add/edit?", preferredStyle: .ActionSheet)
let bgimage = UIAlertAction(title: "Background", style: .Default, handler: { (Alert:UIAlertAction!) -> Void in
print("Edit background image")
})
let logo = UIAlertAction(title: "Logo", style: .Default) { (Alert:UIAlertAction!) -> Void in
print("Edit logo")
}
let cancel = UIAlertAction(title: "Cancel", style: .Cancel) { (Alert:UIAlertAction!) -> Void in
print("remove menu")
}
ActionAlert.addAction(bgimage)
ActionAlert.addAction(logo)
ActionAlert.addAction(cancel)
self.presentViewController(ActionAlert, animated: true, completion: nil)
}
}
如果我不清楚我的问题,请随时询问更多信息
为每个图像创建两个 UIImageView,并将前景一个作为子视图添加到父视图。
像这样:
let bgimg = UIImage(named: "image-name") // The image used as a background
let bgimgview = UIImageView(image: bgimg) // Create the view holding the image
bgimgview.frame = CGRect(x: 0, y: 0, width: 500, height: 500) // The size of the background image
let frontimg = UIImage(named: "front-image") // The image in the foreground
let frontimgview = UIImageView(image: frontimg) // Create the view holding the image
frontimgview.frame = CGRect(x: 150, y: 300, width: 50, height: 50) // The size and position of the front image
bgimgview.addSubview(frontimgview) // Add the front image on top of the background
这是将页脚图片合并到原始图片的示例代码
extension UIImage{
func merge(mergewith:UIImage) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
let actualArea = CGRect(x: 0, y: 0, width: size.width, height: size.height)
let mergeArea = CGRect(x: 0, y: size.height - mergewith.size.height, width: size.width, height: mergewith.size.height)
self.draw(in: actualArea)
mergewith.draw(in: mergeArea)
let merged = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return merged
}
}
我正在尝试制作一个应用程序,用户可以在其中 select 从他们的图片库中选择一张图片作为背景,然后是他们的徽标,他们也可以从他们的(照片)库中选择...
目前我还没有找到可以帮助我的教程,主要是因为用户必须能够设置自己的图像而不是默认图像。 另外,你们中的任何人也知道我如何拥有多个 UIImagePickerControllers 因为这段代码同时影响两个图像而不是每个选择器一个?
我use/have :
我使用 swift
我使用 xcode 7.0.1
这是
the storyboard i currently use.
我的 swift 文件:
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var logo: UIImageView!
@IBOutlet weak var bgimg: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func selectbg(sender: AnyObject) {
let bgPicker = UIImagePickerController()
bgPicker.delegate = self
bgPicker.sourceType = .PhotoLibrary
self.presentViewController(bgPicker, animated: true, completion: nil)
}
@IBAction func selectlogo(sender: AnyObject) {
let logoPicker = UIImagePickerController()
logoPicker.delegate = self
logoPicker.sourceType = .PhotoLibrary
self.presentViewController(logoPicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
logo.image = image
bgimg.image = image
self.dismissViewControllerAnimated(false, completion: nil)
}
@IBAction func addImage(sender: AnyObject) {
let ActionAlert = UIAlertController(title: nil, message: "What image do you want to add/edit?", preferredStyle: .ActionSheet)
let bgimage = UIAlertAction(title: "Background", style: .Default, handler: { (Alert:UIAlertAction!) -> Void in
print("Edit background image")
})
let logo = UIAlertAction(title: "Logo", style: .Default) { (Alert:UIAlertAction!) -> Void in
print("Edit logo")
}
let cancel = UIAlertAction(title: "Cancel", style: .Cancel) { (Alert:UIAlertAction!) -> Void in
print("remove menu")
}
ActionAlert.addAction(bgimage)
ActionAlert.addAction(logo)
ActionAlert.addAction(cancel)
self.presentViewController(ActionAlert, animated: true, completion: nil)
}
}
如果我不清楚我的问题,请随时询问更多信息
为每个图像创建两个 UIImageView,并将前景一个作为子视图添加到父视图。
像这样:
let bgimg = UIImage(named: "image-name") // The image used as a background
let bgimgview = UIImageView(image: bgimg) // Create the view holding the image
bgimgview.frame = CGRect(x: 0, y: 0, width: 500, height: 500) // The size of the background image
let frontimg = UIImage(named: "front-image") // The image in the foreground
let frontimgview = UIImageView(image: frontimg) // Create the view holding the image
frontimgview.frame = CGRect(x: 150, y: 300, width: 50, height: 50) // The size and position of the front image
bgimgview.addSubview(frontimgview) // Add the front image on top of the background
这是将页脚图片合并到原始图片的示例代码
extension UIImage{
func merge(mergewith:UIImage) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
let actualArea = CGRect(x: 0, y: 0, width: size.width, height: size.height)
let mergeArea = CGRect(x: 0, y: size.height - mergewith.size.height, width: size.width, height: mergewith.size.height)
self.draw(in: actualArea)
mergewith.draw(in: mergeArea)
let merged = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return merged
}
}