如何在swift 3中使用imageview嵌套标签
Posted
技术标签:
【中文标题】如何在swift 3中使用imageview嵌套标签【英文标题】:how to nest a label with a imageview in swift 3 【发布时间】:2016-10-19 02:08:54 【问题描述】:以下代码可以拍照并保存到照片库。此代码还具有一个标签,该标签越过图像视图,但它只是越过图像顶部并且没有保存在图像上。如何嵌套标签,以便在保存照片时文本和图像都在照片库的最终照片中。
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate
@IBOutlet weak var image: UIImageView!
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
@IBAction func camera(_ sender: UIButton)
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
label.center = CGPoint(x: 160, y: 222)
label.textAlignment = .center
label.text = "I'am a test label"
self.view.addSubview(label)
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any])
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
image.contentMode = .scaleToFill
image.image = pickedImage
picker.dismiss(animated: true, completion: nil)
@IBAction func save(_ sender: UIButton)
let imageData = UIImagePNGRepresentation(image.image!)
let compressedImage = UIImage(data: imageData!)
UIImageWriteToSavedPhotosAlbum(compressedImage!, nil, nil, nil)
【问题讨论】:
【参考方案1】:你应该用这样的文字来绘制图像:
let str = "I'am a test label" as NSString
let rect = CGRect(origin: CGPoint.zero, size: image.image!.size)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
image.image!.draw(in: rect)
//str.draw(in: <#The Rect You want#>, withAttributes: <#Specify The Font And Color Or Other Property#>)
str.draw(in: CGRect(origin: CGPoint.zero, size: CGSize(width: 200, height: 40)), withAttributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 20), NSForegroundColorAttributeName:UIColor.green])
let img = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
let imageData = UIImagePNGRepresentation(img)
let compressedImage = UIImage(data: imageData!)
UIImageWriteToSavedPhotosAlbum(compressedImage!, nil, nil, nil)
【讨论】:
我不知道在 //str.draw(in: , withAttributes: ) 部分放什么。您能否给我放置位置值以便我可以在照片库中看到附加到图片的一些文字?谢谢 就这样str.draw(in: CGRect(origin: CGPoint.zero, size: CGSize(width: 200, height: 40)), withAttributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 20), NSForegroundColorAttributeName:UIColor.green])
那行不通。我把你的代码都放在我的保存按钮下。照片拍摄并保存,但上面没有文字。
我自己测试了代码。有用。您可以设置更多断点以查看问题所在以上是关于如何在swift 3中使用imageview嵌套标签的主要内容,如果未能解决你的问题,请参考以下文章
如何在Swift中的BezierPath之后为imageView的点1到2和点2到3设置不同的animation.duration时间?
使用 UIPanGestureRecognizer- Swift 3 旋转 ImageView