How to efficiently create a multi-row photo collage from an array of images in Swift

后端 未结 4 531
梦如初夏
梦如初夏 2020-12-17 17:01

Problem

I am building a collage of photos from an array of images that I am placing onto a tableview. I want to make the images wrap when the number

4条回答
  •  渐次进展
    2020-12-17 17:10

    Since you're using Swift 2.0: UIStackView does exactly what you're trying to do manually, and is significantly easier to use than UICollectionView. Assuming you're using Storyboards, creating a prototype TableViewCell with multiple nested UIStackViews should do exactly what you want. You'll just need to make sure that the UIImages you're inserting are all the same aspect ratio if that's what you want.

    Your algorithm is highly inefficient because it has to re-draw, with multiple Core Animation transforms, every image any time you add or remove an image from your array. UIStackView supports dynamically adding and removing objects.

    If you still, for some reason, need to snapshot the resulting collage as a UIImage, you can still do this on the UIStackView.

提交回复
热议问题