我有一个图像视图来显示相机的结果,但问题是当我拍照时,结果是图像将被缩放。
这是我的代码:

var userData2 = userData()

var base64String1: String!
var decodedData1: Data!
var decodedImage1: UIImage!
@IBOutlet weak var imageResult: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    base64String1 = userData2.fotoIDString!
    decodedData1 = Data(base64Encoded: base64String1, options: [])!
    decodedImage1 = UIImage(data: decodedData1)!
//    decodedImage1 = Toucan(image: decodedImage1).resize(CGSize(width: 100, height: 150)).maskWithEllipse().image
    imageResult.image = decodedImage1
    imageResult.transform = CGAffineTransform(rotationAngle: CGFloat.pi/2)
    imageResult.contentMode = .scaleAspectFill
}

最佳答案

您需要将imageView的contentMode.scaleAspectFill更改为.scaleAspectFit
请尝试以下代码:

var userData2 = userData()

var base64String1: String!
var decodedData1: Data!
var decodedImage1: UIImage!
@IBOutlet weak var imageResult: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    base64String1 = userData2.fotoIDString!
    decodedData1 = Data(base64Encoded: base64String1, options: [])!
    decodedImage1 = UIImage(data: decodedData1)!
//    decodedImage1 = Toucan(image: decodedImage1).resize(CGSize(width: 100, height: 150)).maskWithEllipse().image
    imageResult.image = decodedImage1
    imageResult.transform = CGAffineTransform(rotationAngle: CGFloat.pi/2)
    imageResult.contentMode = .scaleAspectFit//here is the difference
    imageResult.clipsToBounds = true//also add this line


}

10-07 16:21