3

I'm a beginner with swift and I can't get the resize of a UIImageView working.

Here is my current layout :

Current layout

What I want is to resize the images to occupy half the screen's width (2 images per row).

Here is my storyboard :

storyboard

Here is the function drawing the collection view inside my controller :

func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {
        var cell : MenuInspirationCellView = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as MenuInspirationCellView

        cell.imageView.layer.borderWidth = 2
        cell.imageView.layer.borderColor = UIColor.yellowColor().CGColor

        //This is my first approach trying to modify the frame :
        cell.imageView.frame = CGRectMake(0,0, self.view.bounds.width / 2,v120)
        var cellImage : UIImage = UIImage(data: NSData(contentsOfURL: NSURL(string:              images[indexPath.row]))) 
        cell.imageView.image = cellImage;

        //This is my second approach (based on http://www.snip2code.com/Snippet/89236/Resize-Image-in-iOS-Swift) :


        // to resize an image to dimension 52x52
        //var newSize:CGSize = CGSize(width: 52,height: 52)
        //let rect = CGRectMake(0,0, newSize.width, newSize.height)
        //UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
        // image is a variable of type UIImage
        //cellImage.drawInRect(rect)
        //let newImage = UIGraphicsGetImageFromCurrentImageContext()
        //UIGraphicsEndImageContext()
        // resized image is stored in constant newImage
        //cell.imageView.image = newImage;


        //This is my thrid approach (based on https://gist.github.com/hcatlin/180e81cd961573e3c54d, of course i added his functions but I don't show them here for the sake of readability) :
         //cell.imageView.image = self.RBSquareImageTo(cellImage, size: CGSize(width: 80, height: 80))

        return cell
    }

Any leads to sort this out ?

Alexandre Nucera
  • 2,183
  • 2
  • 21
  • 34

3 Answers3

4

SWIFT:

func imageResize(imageObj:UIImage, sizeChange:CGSize)-> UIImage {

    let hasAlpha = false
    let scale: CGFloat = 0.0 // Automatically use scale factor of main screen

    UIGraphicsBeginImageContextWithOptions(sizeChange, !hasAlpha, scale)
    imageObj.drawInRect(CGRect(origin: CGPointZero, size: sizeChange))

    let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext() // !!!
    return scaledImage
}
Alexander Volkov
  • 7,904
  • 1
  • 47
  • 44
3

The sizes of your cells are decided by your UICollectionViewLayout object, which is a UICollectionViewFlowLayout in your case. If you want all of your cells to be the same size, you can configure the properties on the layout itself; itemSize and minimumInterimSpacing will do the trick. Alternatively, if you want your cells to be different sizes, say depending on the image each contains or whatever, you needs your collection view delegate to implement the UICollectionViewDelegateFlowLayout method:

optional func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize

returning the size you want for each cell.

stefandouganhyde
  • 4,494
  • 1
  • 16
  • 13
  • Thanks ! Now I'm able to dynamically resize my cells. But how could I resize the image inside each cell ? – Alexandre Nucera Sep 03 '14 at 14:01
  • No problem, @AlexandreNucera. The image inside each cell can be resized by adjusting the various autolayout constraints in your xib or in the code, setting the `UIImageView`'s `autoresizingMask` property, or manually doing the layout in `MenuInspirationCellView`'s `layoutSubviews` function. – stefandouganhyde Sep 03 '14 at 17:41
  • Thank you this is exactly what I needed. I got this working by modifying my constraints (I removed the fixed 120 width and height). – Alexandre Nucera Sep 04 '14 at 09:56
1

Following this tutorials you can resize UIImage, In that tutorial described fully .. Image Resize in swift ios8

    func imageResize (imageObj:UIImage, sizeChange:CGSize)-> UIImage{

     let hasAlpha = false
    let scale: CGFloat = 0.0 // Automatically use scale factor of main screen

    UIGraphicsBeginImageContextWithOptions(sizeChange, !hasAlpha, scale)
    imageObj.drawInRect(CGRect(origin: CGPointZero, size: sizeChange))

    let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
    return scaledImage
}

Here you can also see many tutorials about Swift language Visit Here iPhone & iPad Application Development Help World

Jogendra.Com
  • 6,394
  • 2
  • 28
  • 35