繁体   English   中英

Swift 3-UIImageView图像缩小

[英]Swift 3 - UIImageView image gets shrunk

这是准备好序列的代码,然后从该序列中解散出来的代码。

    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时,它会严重缩水。 在对图像进行标记和标记之前:

在PhotoFine之前

然后,当我们用标签标记照片后返回该VC时,这是紧缩后的照片:

AfterPhotoShrunk

在搜索了StackOverflow和其他地方几个小时之后,解决该问题的最佳建议是将contentMode设置为.scaleAspectFit,但这没有用(并且无论如何我都希望将其保留为scaleToFill,无论如何在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!
}

好的-看来这是问题所在...

您的TossImageViewController有一个UIImageView我不知道它的大小,但可以说它是300x300。 您设置其.image = displayedImage 到目前为止,一切都很好...您会看到图像缩放到适合屏幕大小的大小。

接下来,在用户交互之后,您调用textToImage(...)而我您正在尝试将文本绘制原始图像上吗? 如果是这样,那不是正在发生的事情。

相反,您将UILabel添加到self.view ,创建3000 x 2002的Graphics Context,然后返回UIGraphicsGetImageFromCurrentImageContext() ...但是该图像是当前视图-可能是750 x 1334 ,位于上方,上下文的左上角。

您需要做的是:

a)创建一个3000 x 2002 UIView,在UIImageView中将图像添加为该视图的子视图,然后将UILabel(相对于大图像调整大小)添加为另一个子视图。 然后从上下文中获取图像。 要么...

b)使用上下文和图像drawInRect和字符串drawInRect函数。

这篇文章有一些讨论和解决方案(Obj-C和Swift): 如何在Objective-C(iOS)中的图像上编写文本?

暂无
暂无

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

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