gpt4 book ai didi

swift - 如何让用户上传不同尺寸的照片?

转载 作者:行者123 更新时间:2023-11-30 10:27:54 27 4
gpt4 key购买 nike

Swift 世界的新手。

我已经使用UIImagePickerController实现了用户上传图片的功能。但是,我对要上传的图像的尺寸有点困惑。

人们可以在 Pinterest 中上传不同尺寸的图像,因此在主页中,我们看到一个美丽的瀑布 Collection View (单元格的大小由图像的高度决定)。

Pinterest App 在中国 super 慢,让我以类似的 App 为例。正如您所看到的, Collection View 中每个单元格的大小都是不同的。

enter image description here

每个单元格的大小由图像的高度决定。例如:

extension MainViewController: PinterestLayoutDelegate {
func collectionView(
_ collectionView: UICollectionView,
heightForPhotoAtIndexPath indexPath:IndexPath) -> CGFloat {
return photos[indexPath.item].image.size.height
}
}

为了实现此 View ,应用程序必须允许用户上传不同尺寸的图像。

在我的 Storyboard中,我创建了一个 ImageView 和一个“上传图像”按钮。

当按下按钮时,将调用以下函数:

 func presentImagePickerController(with sourceType: UIImagePickerController.SourceType, from viewController: UIViewController) {
let imagePickerController = UIImagePickerController()
imagePickerController.sourceType = sourceType
imagePickerController.allowsEditing = true
imagePickerController.delegate = self

viewController.present(imagePickerController, animated: true)
}

ImageView 的内容模式设置为“缩放以适合”。然而, ImageView 的尺寸是固定的。

如果我们看同一个应用程序,当人们上传图像时,它会要求人们从原始大小方形自定义/中选择一个选项裁剪:

enter image description here

这是否意味着我需要创建具有不同尺寸的不同 ImageView 并让用户选择他们想要的尺寸?

或者一般来说,如何让用户上传不同尺寸的图片?

提前致谢。

最佳答案

如果您想与应用程序共享图像,请使用以下代码:

let activity = UIActivityViewController(activityItems: [yourImage], applicationActivities: nil)
activity.popoverPresentationController?.sourceView = self.view

self.present(activity,animated: true, completion: nil)

当您调用此方法时,将呈现共享 View 。简单的如果您有任何问题,请告诉我。填写免费

关于swift - 如何让用户上传不同尺寸的照片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59746505/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com