我的应用程序中有一个UIImageView,我试图让它显示一个宽阔的短片。我正在尝试保留原始图像的宽高比。
这是我的代码。它目前无法按我希望的方式工作:
func scaleImage(oldImage: UIImage) -> UIImage {
// 1. Use only y=273 to y=299
let x = 0
let y = Int(oldImage.size.height/2)
let width = Int(oldImage.size.width)
let height = 150
let cropRect = CGRect(x: y, y: x, width: height, height: width)
let cgImage = CGImageCreateWithImageInRect(oldImage.CGImage, cropRect)
var image = UIImage(CGImage: cgImage!, scale:oldImage.scale, orientation: oldImage.imageOrientation)
UIImageWriteToSavedPhotosAlbum(oldImage, nil, nil, nil)
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
imageView.translatesAutoresizingMaskIntoConstraints = true
imageView.image = image
imageView.contentMode = UIViewContentMode.ScaleAspectFit
imageView.frame.size.height = image.size.height
}
这是保存到相机胶卷的已编辑图像:http://i.imgur.com/3FeNCMh.png
这是UIImageView上显示的内容:http://i.imgur.com/FCgeUJD.png
如您所见,图像在两侧被切掉。我在这里缺少什么?