My code below is supposed to feature a customizable border for a photo taken. I originally had a border but it was not customizable. I saw this code online about a customizable border in a extension file (CALAYER). Its obviously to me I have to call the CALYAER function in my class file (x1ViewController) for the border to appear I just don’t know how to do it.
import UIKit
extension CALayer {
func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {
let border = CALayer();
switch edge {
case UIRectEdge.top:
border.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: thickness)
break
case UIRectEdge.bottom:
border.frame = CGRect(x:0, y:self.frame.height - thickness, width:self.frame.width, height:thickness)
break
case UIRectEdge.left:
border.frame = CGRect(x:0, y:0, width: thickness, height: self.frame.height)
break
case UIRectEdge.right:
border.frame = CGRect(x:self.frame.width - thickness, y: 0, width: thickness, height:self.frame.height)
break
default:
break
}
border.backgroundColor = color.cgColor;
self.addSublayer(border)
}
}
class x1ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate {
@IBOutlet weak var imageDisplay: UIImageView!
var screenView: UIImageView!
@IBAction func camera(_ sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
// imageDisplay.layer.borderColor = UIColor(red: 0, green: 1.0, blue: 0, alpha: 1).cgColor
//imageDisplay.layer.cornerRadius = 1
// imageDisplay.contentMode = .scaleAspectFit
// imageDisplay.layer.borderWidth = 1
imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)}}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject: AnyObject]!){
imageDisplay.image = image
self.dismiss(animated: true, completion: nil);
screenView = UIImageView()
screenView.frame = CGRect(x:0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
let text = "#HAPPY"
let label = UILabel(frame: CGRect(x: 125, y: 700, width: self.view.frame.width, height: 300))
label.font = UIFont(name: label.font.fontName, size: 122)
label.textColor = UIColor.blue
label.alpha = 1.0
label.text = text
self.view.addSubview(screenView)
self.view.addSubview(label)
UIGraphicsBeginImageContext(self.imageDisplay.frame.size)
self.view.drawHierarchy(in: self.view.frame, afterScreenUpdates: true)
}
@IBAction func save(_ sender: AnyObject) {
let alert = UIAlertController(title: "Image Saved", message: "Image is in photo gallery", preferredStyle: .alert)
let okay = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(okay)
present(alert, animated: true, completion: nil)
let photo = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageWriteToSavedPhotosAlbum(photo!, nil, nil, nil)
}
}