我正在开发一款试图为照片制作可自定义边框大小的应用。所以边框中的所有边都不会是相同的宽度。我在线获得扩展ca层,并被告知它允许我创建一个边框,每边都有一个可自定义的宽度。我只是不知道代码ca层在哪里。
import UIKit
class x1ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate {
@IBOutlet weak var imageDisplay: UIImageView!
var screenView: UIImageView!
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)
}
}
@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)
}
}