这是为segue准备的代码,然后从该segue中展开。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toTossImageVC" {
let dvc = segue.destination as! TossImageViewController
dvc.displayedImage = tossPhoto2.image
}
}
@IBAction func unwindToTossVC(unwindSegue: UIStoryboardSegue) {
let dvc = unwindSegue.source as! TossImageViewController
self.tossPhoto2.image = dvc.displayedImage
}
可以看出,我正在准备中将图像传递到目标VC,然后我在unwind方法中检索该图像的更新版本(用户可以在目标VC中标记图像)。
self.tossPhoto2是一个UIImage对象,其contentMode在Interface Builder中设置为scaleToFill。我的问题是,当我放松并将tossPhoto2的图像分配给dvc.displayedImage时,它会变得非常缩小。在对图像进行分割和标记之前:
然后,当我们用标签标记照片后返回此VC时,这是可怕的缩小照片:
在StackOverflow和其他地方搜索了几个小时之后,解决问题的最佳建议是将contentMode设置为.scaleAspectFit,但这不起作用(我想将它保持为scaleToFill,无论如何在Interface Builder中定义) )。
为什么会发生这种情况以及如何解决这个问题?这是TossImageViewController中的代码,如果它有帮助,但我不知道如何无意中缩小图像。这可以允许用户将标签(实现为UILabel)添加到图像上。
class TossImageViewController: UIViewController, UINavigationControllerDelegate, UIGestureRecognizerDelegate {
@IBOutlet var tossImage: UIImageView!
var displayedImage: UIImage!
let ac = UIAlertController(title: "Select Toss Type", message: nil, preferredStyle: .actionSheet)
var selectedTossType = String()
var touchPoint: CGPoint!
override func viewDidLoad() {
super.viewDidLoad()
tossImage.image = displayedImage
self.tabBarController?.tabBar.isHidden = true
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
self.view.addGestureRecognizer(gestureRecognizer)
// Set up the action sheet picker
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
ac.addAction(cancelAction)
let trashAction = UIAlertAction(title: "Trash", style: .destructive, handler: {(action) -> Void in
self.selectedTossType = "Trash"
// Write the selected toss type where the tap happened
self.displayedImage = self.textToImage(drawText: self.selectedTossType as NSString, inImage: self.displayedImage, atPoint: self.touchPoint, labelWidth: 80)
print("Trash tag")
})
ac.addAction(trashAction)
let sinkAction = UIAlertAction(title: "Sink Disposal", style: .destructive, handler: {(action) -> Void in
self.selectedTossType = "Sink Disposal"
// Write the selected toss type where the tap happened
self.displayedImage = self.textToImage(drawText: self.selectedTossType as NSString, inImage: self.displayedImage, atPoint: self.touchPoint, labelWidth: 120)
})
ac.addAction(sinkAction)
let saveAction = UIAlertAction(title: "Save for Later", style: .destructive, handler: {(action) -> Void in
self.selectedTossType = "Save for Later"
// Write the selected toss type where the tap happened
self.displayedImage = self.textToImage(drawText: self.selectedTossType as NSString, inImage: self.displayedImage, atPoint: self.touchPoint, labelWidth: 140)
})
ac.addAction(saveAction)
let animalAction = UIAlertAction(title: "Animal", style: .destructive, handler: {(action) -> Void in
self.selectedTossType = "Animal"
// Write the selected toss type where the tap happened
self.displayedImage = self.textToImage(drawText: self.selectedTossType as NSString, inImage: self.displayedImage, atPoint: self.touchPoint, labelWidth: 80)
})
ac.addAction(animalAction)
let sharingAction = UIAlertAction(title: "Sharing", style: .destructive, handler: {(action) -> Void in
self.selectedTossType = "Sharing"
// Write the selected toss type where the tap happened
self.displayedImage = self.textToImage(drawText: self.selectedTossType as NSString, inImage: self.displayedImage, atPoint: self.touchPoint, labelWidth: 80)
})
ac.addAction(sharingAction)
let compostAction = UIAlertAction(title: "Compost", style: .destructive, handler: {(action) -> Void in
self.selectedTossType = "Compost"
// Write the selected toss type where the tap happened
self.displayedImage = self.textToImage(drawText: self.selectedTossType as NSString, inImage: self.displayedImage, atPoint: self.touchPoint, labelWidth: 80)
})
ac.addAction(compostAction)
}
override func viewWillDisappear(_ animated: Bool) {
self.tabBarController?.tabBar.isHidden = false
}
@IBAction func cancel(_ sender: Any) {
navigationController!.popViewController(animated: true)
}
func textToImage(drawText text: NSString, inImage image: UIImage, atPoint point: CGPoint, labelWidth: CGFloat) -> UIImage {
let label = UILabel(frame: CGRect(x: (point.x - labelWidth/2), y: point.y, width: labelWidth, height: 20))
label.textColor = .red
label.shadowColor = .yellow
label.text = text as String
label.textAlignment = .center
self.view.addSubview(label)
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(image.size, false, scale)
view.layer.render(in: UIGraphicsGetCurrentContext()!)
let taggedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return taggedImage!
}
答案 0 :(得分:0)
好的 - 看起来这就是问题......
UIImageView
中有TossImageViewController
- 我不知道它的大小,但我们说它是300x300。您设置了.image = displayedImage
。到目前为止,这么好......你看到你的图像缩放到适合屏幕。
接下来,在用户互动后,您拨打textToImage(...)
并且我猜测您正在尝试将文本绘制到原始图片上?如果是这样,那不是发生了什么。
相反,您要向UILabel
添加self.view
,创建3000 x 2002
的图形上下文,然后返回UIGraphicsGetImageFromCurrentImageContext()
...但该图像是当前视图 - 也许是750 x 1334
并且位于上下文的左上角。
您需要做的是:
a)创建一个3000 x 2002
UIView,将图像作为该视图的子视图添加到UIImageView中,然后将UILabel(相对于大图像调整大小)添加为另一个子视图。然后从那个上下文中获取图像。还是......
b)使用Context和image drawInRect和字符串drawInRect函数。
这篇文章有一些讨论和解决方案(Obj-C和Swift):How to write text on image in Objective-C (iOS)?