這裏是代碼,準備一個segue,然後從那個segue展開。斯威夫特3 - UIImageView圖像得到收縮
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的圖像中的準備,然後我檢索該圖像的更新版本中(用戶可以標記在目的地VC圖像)放鬆的方法。
self.tossPhoto2是一個UIImage對象,它的contentMode在Interface Builder中設置爲scaleToFill。我的問題是,當我放鬆並將tossPhoto2的圖像分配給dvc.displayedImage時,它會變得非常縮小。之前segueing和標記的圖像:
然後,當我們標註與標籤的照片後返回到這個VC,這裏的後可怕的縮水照片:
搜索StackOverflow的後和其他地方几個小時,解決這個問題的最好建議是將contentMode設置爲.scaleAspectFit,但這不起作用(並且我希望將它作爲InterfaceToFill在Interface Builder中定義)。
任何想法,爲什麼發生這種情況,我該如何解決這個問題?這是TossImageViewController中的代碼,如果它有幫助的話,但我不明白我可能會無意中縮小圖像。這可以允許用戶添加標籤(實現爲UILabels)到圖像上。
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!
}
的'contentMode'不*不*改變形象 - 它只是改變了它是**顯示方式**。我猜'TossImageViewController'中的某些內容正在修改圖像,並且,如果您沒有故意這麼做,則會創建一個* small *版本,然後返回該圖像。 – DonMag
瞭解contentMode。根據你的建議,我在整個執行過程中(在兩個風險投資公司之間)檢查了image.size,並且從未改變:到2002年的最高值爲3000。仍在挖掘一個答案。如果有幫助,我可以在TossImageViewController中共享代碼。 – Yorma