[英]Swift/iOS - Save Image with given aspect ratio
我已经在网上搜索了几个小时,试图寻找解决方案,但是没有运气。
我已经阅读了一些已经提出的主题并接受了建议,但是没有看。
我已经在项目中安装了CropViewController
容器,并且图像/图像预览的纵横比为9:16,非常完美。 问题是当我希望将此图像保存到用户照片库时。 这是一样的/普通纵横比Original
。
这是我的代码。 我在一个视图控制器中具有相机功能,在另一个视图控制器中具有图像的呈现和保存功能。
import UIKit
import CropViewController
class CameraPreviewController: UIViewController {
var image: UIImage!
@IBOutlet var imagePreview: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imagePreview.image = image
}
@IBAction func cancelButton_TouchUpInside(_ sender: Any) {
navigationController?.popViewController(animated: true)
}
@IBAction func saveButton_TouchUpInside(_ sender: Any) {
let croppedImage: UIImage = self.image
let cropViewController = CropViewController(image: croppedImage)
cropViewController.customAspectRatio = CGSize(width: 9.0, height: 16.0)
self.image = croppedImage
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
navigationController?.popViewController(animated: true)
}
}
如何保存屏幕上的图像? 我已经尝试过但是没有运气。 图像为9:16,但保存时出现问题。 我该如何克服这个问题?
谢谢。
您应该在info.plist中添加“隐私-照片库添加用法说明”。 尝试这种方法,希望对您没有帮助,我会尝试寻找另一种方法。
func cropViewController(_ cropViewController: CropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int) {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
找到了解决方案。
更改了这一行代码-
captureSession.sessionPreset = .photo
到以下内容:
captureSession.sessionPreset = .iFrame1280x720
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.