如何在 swift 中为 uiimageview 制作自定义边框
Posted
技术标签:
【中文标题】如何在 swift 中为 uiimageview 制作自定义边框【英文标题】:How to make a custom border for a uimmageview in swift 【发布时间】:2016-12-05 16:36:57 【问题描述】:我希望底部边框高度比其余边框高得多,以使图像看起来像宝丽来照片。现在我的代码 a 的所有 4 个边都有一个标准边框长度。我不希望图像被裁剪我只希望底部边框的高度更高。下面是我的代码。这是代码结果的图片。
import UIKit
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)
【问题讨论】:
您需要添加另一个UIView
作为UIImageView
的子视图,并使用其frame
属性将其放置在底部。
这是一个很好的例子,可以为视图添加边框。 ***.com/a/30519213/2618600
【参考方案1】:
您可以通过覆盖 rectdraw 方法在视图中绘制,然后将其添加到图像视图上方。 这里是苹果文档:https://developer.apple.com/library/content/documentation/2DDrawing/Conceptual/DrawingPrintingios/BezierPaths/BezierPaths.html
有一个工具可用于通过鼠标拖动点来执行 rectdraw 代码,该工具很有用。 https://www.paintcodeapp.com/ 您可以免费试用。
【讨论】:
以上是关于如何在 swift 中为 uiimageview 制作自定义边框的主要内容,如果未能解决你的问题,请参考以下文章
Swift3 淡入/淡出或平滑过渡 UIImageView animationImages
如何在 UIView 中为 UIImageView 添加 tapGesture
如何在 UITableView 的单元格中为 UIImageView 获取 touchesMoved?