Swift世界的新手。
我已经实现了用户上传图片使用UIImagePickerController
的功能。然而,我对要上传的图片的尺寸有点困惑。
人们可以在Pinterest中上传不同尺寸的图片,所以在主页上,我们可以看到一个漂亮的瀑布集合视图(单元格的大小由图片的高度决定)。
Pinterest App在中国的速度非常慢,让我用一个类似的App作为例子。正如您所看到的,Collection View中每个单元格的大小是不同的。
每个单元格的大小由图像的高度决定。例如:
extension MainViewController: PinterestLayoutDelegate {
func collectionView(
_ collectionView: UICollectionView,
heightForPhotoAtIndexPath indexPath:IndexPath) -> CGFloat {
return photos[indexPath.item].image.size.height
}
}
为了实现这个视图,应用程序必须允许用户上传不同尺寸的图像。
在我的故事板中,我创建了一个Image视图和一个"upload image“按钮。
当按下按钮时,将调用以下函数:
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)
}
Image View的内容模式设置为“缩放以适应”。但是,图像视图的尺寸是固定的。
如果我们看看同一个应用,当人们上传一张图片时,它会要求人们从original size
、square
和customized/crop
中选择一个:
这是否意味着我需要创建具有不同尺寸的不同图像视图,并让用户选择他们想要的尺寸?
或者一般情况下,如何让用户上传不同尺寸的图片?
提前谢谢。
发布于 2020-01-15 15:50:34
如果你想将图片分享给应用程序,请使用以下代码:
let activity = UIActivityViewController(activityItems: [yourImage], applicationActivities: nil)
activity.popoverPresentationController?.sourceView = self.view
self.present(activity,animated: true, completion: nil)
调用此函数时,将显示共享视图。如果你有任何问题,请告诉我。自由填充
https://stackoverflow.com/questions/59746505
复制相似问题