2017-08-08 57 views
0

這裏是代碼,準備一個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和標記的圖像:

BeforePhotoFine

然後,當我們標註與標籤的照片後返回到這個VC,這裏的後可怕的縮水照片:

AfterPhotoShrunk

搜索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! 
} 
+1

的'contentMode'不*不*改變形象 - 它只是改變了它是**顯示方式**。我猜'TossImageViewController'中的某些內容正在修改圖像,並且,如果您沒有故意這麼做,則會創建一個* small *版本,然後返回該圖像。 – DonMag

+0

瞭解contentMode。根據你的建議,我在整個執行過程中(在兩個風險投資公司之間)檢查了image.size,並且從未改變:到2002年的最高值爲3000。仍在挖掘一個答案。如果有幫助,我可以在TossImageViewController中共享代碼。 – Yorma

回答

0

確定 - 它看起來像這樣的問題......

你必須在你的UIImageViewTossImageViewController - 我不知道它的大小,但讓我們說這是300×300。您設置了.image = displayedImage。到目前爲止,這麼好......你看到你的圖像縮放以適應屏幕。

接下來,用戶交互之後,你叫textToImage(...),我猜正在努力文本繪製到原始圖像?如果是這樣,那不是發生了什麼事。

相反,你要添加UILabelself.view,創造3000 x 2002圖形上下文,然後返回UIGraphicsGetImageFromCurrentImageContext() ...但是,圖像是當前視圖 - 這也許是750 x 1334和坐在的的左上角語境。

你需要做的是,無論是:

一)創建一個3000 x 2002的UIView,在一個UIImageView添加圖像作爲這一觀點的子視圖,然後添加的UILabel(尺寸相對於大的圖像)作爲另一個子視圖。然後從得到上下文的圖片。或...

b)使用Context和圖像drawInRect和字符串drawInRect函數。

這個職位有一些討論和解決方案(包括對象 - 和SWIFT):How to write text on image in Objective-C (iOS)?

+0

謝謝!這讓我越過了駝峯 - 我結束了在你提供的鏈接中的討論,並且我現在(非常接近)瞭解我正在尋找的確切行爲。對此,我真的非常感激! – Yorma

+0

DonMag - 在跟蹤鏈接到的SO帖子中的代碼後,我可以在不縮小圖像的情況下工作,所有這些都很好。但是現在文本並沒有顯示出用戶點擊的位置,正如我在這篇文章中所描述的:https://stackoverflow.com/questions/45580224/swift-3-nsstring-drawin-rect-withattributes-text-not-being -drawn-AT-EX。任何想法,爲什麼發生這種情況? – Yorma