如何在Swift中将照片调整为3种尺寸(纵横比)

时间:2018-08-23 09:44:04

标签: ios iphone swift xcode uiimage

我正在用Swift制作一个调整大小的照片应用程序。我希望它能够在您的iPhone上拍照并在应用程序的相机胶卷中选择一张照片。最后,您可以选择一种尺寸并在该尺寸上获取一张新照片。

您可以选择的尺寸是:

  • 3.0 x 4.0
  • 2.4 x 3.0
  • 3.5 x 4.5

最接近我的理想的应用是:
Image Size
这个应用程式可以随意调整相片大小。

使我感到困扰的是:

  • 根据设备的不同,照片尺寸也会改变。
  • 前置摄像头和后置摄像头的照片尺寸不同

例如,即使您可以在2.4x3.0中正确调整iPhone 6相机胶卷中的照片大小,也无法在3.0x4.0中正确调整照片大小。最终图片看起来在纵向延伸。

如果有人想在可选方面调整大小,您能告诉我吗?

谢谢。

调整为3.0x4.0

let cgImage = size1Image.cgImage!.cropping(to: CGRect(x: 0, y: 0, width: 740, height: 720/3.0*4.0))
let croppingImage = UIImage(cgImage: cgImage!)
var resize = croppingImage.ResizeUIImage(width: 353, height: 470)

调整为2.4x3.0

let cgImage = size2Image.cgImage!.cropping(to: CGRect(x: 0, y: 0, width: 720*4.2, height: 720/2.4*3.0*4.0))
let croppingImage = UIImage(cgImage: cgImage!)        
let resize = croppingImage.ResizeUIImage(width: 283, height: 354)

调整为3.5x4.5

let cgImage = size3Image.cgImage!.cropping(to: CGRect(x: 0, y: 0, width: 720*4.2, height: 720/3.5*4.5*4.0))
let croppingImage = UIImage(cgImage: cgImage!)        
let resize = croppingImage.ResizeUIImage(width: 402, height: 531)

ResizeImage

extension UIImage {
    func ResizeUIImage(width : CGFloat, height : CGFloat)-> UIImage!{
        UIGraphicsBeginImageContext(CGSize(width: width, height: height))
        self.draw(in: CGRect(x: 0, y: 0, width: width, height: height))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage
    }
}  

0 个答案:

没有答案