简体   繁体   English

Swift 3-UIImageView图像缩小

[英]Swift 3 - UIImageView image gets shrunk

Here is code that prepares-for a segue, and then unwinds from that 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
}

As can be seen, I am passing an image to the destination VC in the prepare for, and then I am retrieving an updated version of that image (the user can tag the image in the destination VC) in the unwind method. 可以看出,在准备过程中将图像传递到目标VC,然后以展开方法检索该图像的更新版本(用户可以在目标VC中标记该图像)。

self.tossPhoto2 is a UIImage object and its contentMode is set to scaleToFill in Interface Builder. self.tossPhoto2是一个UIImage对象,并且它的contentMode在Interface Builder中设置为scaleToFill。 My problem is that when I do the unwind and assign tossPhoto2's image to dvc.displayedImage, it gets horribly shrunk. 我的问题是,当我展开并分配tossPhoto2的图像到dvc.displayedImage时,它会严重缩水。 Before segueing and tagging the image: 在对图像进行标记和标记之前:

BeforePhotoFine 在PhotoFine之前

Then, when we return to this VC after tagging the photo with labels, here's the horribly shrunk photo after: 然后,当我们用标签标记照片后返回该VC时,这是紧缩后的照片:

AfterPhotoShrunk AfterPhotoShrunk

After searching StackOverflow and other places for a couple of hours, the best suggestion to solving the problem was to set contentMode to .scaleAspectFit but that didn't work (and I want to keep this as scaleToFill anyway as defined in Interface Builder). 在搜索了StackOverflow和其他地方几个小时之后,解决该问题的最佳建议是将contentMode设置为.scaleAspectFit,但这没有用(并且无论如何我都希望将其保留为scaleToFill,无论如何在Interface Builder中定义)。

Any ideas why this is happening and how I can fix this? 有什么想法为什么会发生以及如何解决这个问题? Here's the code in TossImageViewController if it helps, but I don't see how I might be inadvertently shrinking the image. 这是TossImageViewController中的代码(如果有帮助的话),但是我看不到如何无意间缩小图像。 This could allows the user to add tags (implemented as UILabels) onto the image. 这可以允许用户将标签(实现为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!
}

OK - it looks like this is the problem... 好的-看来这是问题所在...

You have a UIImageView in your TossImageViewController - I don't know its size, but let's say it's 300x300. 您的TossImageViewController有一个UIImageView我不知道它的大小,但可以说它是300x300。 You set its .image = displayedImage . 您设置其.image = displayedImage So far, so good... you see your image scaled to fit on the screen. 到目前为止,一切都很好...您会看到图像缩放到适合屏幕大小的大小。

Next, after user interaction, you call textToImage(...) and I'm guessing you are trying to draw the text onto the original image? 接下来,在用户交互之后,您调用textToImage(...)而我您正在尝试将文本绘制原始图像上吗? If so, that's not what's happening. 如果是这样,那不是正在发生的事情。

Instead, you are adding a UILabel to self.view , creating a Graphics Context of 3000 x 2002 , and then returning UIGraphicsGetImageFromCurrentImageContext() ... but that image is the current view - which is perhaps 750 x 1334 and sitting in the upper-left corner of the Context. 相反,您将UILabel添加到self.view ,创建3000 x 2002的Graphics Context,然后返回UIGraphicsGetImageFromCurrentImageContext() ...但是该图像是当前视图-可能是750 x 1334 ,位于上方,上下文的左上角。

What you need to do is, either: 您需要做的是:

a) create a 3000 x 2002 UIView, add the image in a UIImageView as a subview of that view, then add the UILabel (sized relative to the big image) as another subview. a)创建一个3000 x 2002 UIView,在UIImageView中将图像添加为该视图的子视图,然后将UILabel(相对于大图像调整大小)添加为另一个子视图。 Then get the image from that context. 然后从上下文中获取图像。 Or... 要么...

b) use the Context and image drawInRect and string drawInRect functions. b)使用上下文和图像drawInRect和字符串drawInRect函数。

This post has some discussion and solutions (both Obj-C and Swift): How to write text on image in Objective-C (iOS)? 这篇文章有一些讨论和解决方案(Obj-C和Swift): 如何在Objective-C(iOS)中的图像上编写文本?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM