0

I am putting images with UICollectionView.
I would like to arrange all the images under the following conditions.
(It's like ios filemanager)

1. AspectFit
2. Bottom Alignment
3. with Corner Radius

This is a reference image.

Here is image

What kind of code should be written with imageView to achieve this?

rmaddy
  • 298,130
  • 40
  • 468
  • 517
PERIPERI
  • 65
  • 7
  • fyi, you can achieve all of this via StoryBoard – heximal Jan 30 '18 at 14:51
  • Thanks!! If possible please tell me the setting items and contents on the Story board. – PERIPERI Jan 30 '18 at 14:54
  • Sorry I forgot to mention, but this takes into account the case where the image was dynamically generated. – PERIPERI Jan 30 '18 at 14:55
  • sorry, I misinformed you, the objective looks more complicated than I thought – heximal Jan 30 '18 at 15:03
  • I think aspect fit and corner should be fine. I assume all the image contained in a same size container. For bottom alignment, one way I can think of is to check the UIImage's size. For vertical image, you use vertical image cell and horizontal image you use horizontal image cell. Vertical image cell has top, bottom and centerX constraint. Horizontal image cell has bottom, leading and trailing constraint. They both have scaleAspectFit mode. – Surely Jan 30 '18 at 15:03
  • Check this example: https://stackoverflow.com/questions/31735228/how-to-make-a-simple-collection-view-with-swift FYI try to share code or at least what you have tried next time. –  Jan 30 '18 at 15:09
  • Have you tried to create a custom cell? – GuiDupas Jan 30 '18 at 15:11
  • Thanks everyone. I try now. @GuiDupas Yeah, but i think that's for MVC model. if u know another usage for result my Q, plz tell me ;_; – PERIPERI Jan 30 '18 at 16:34

1 Answers1

0

Thanks everyone. I solved the problem with a very simple calculation.

//swift

/*
cW = UIImage's width
cH = UIImage's height
cS = UIViewImage's height or width(my case is square. so whichever is fine)
*/

func resizeImageView(cW w:CGFloat, cH h:CGFloat, cS s:CGFloat) -> (a:CGFloat, b:CGFloat) {
    if w > h{
        let a = (h * s) / w
        let b = (s - a)
        return (CGFloat(a), CGFloat(b))
    }else{
        let a = (w * s) / h
        let b = (s - a) / 2.0000
        return (CGFloat(a), CGFloat(b))
    }
}

After that, change the width, height, x, y of imageView using each value.

PERIPERI
  • 65
  • 7