简体   繁体   中英

How to resize an UIImageView (swift)

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

Here is my 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 :

故事板

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 ?

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.

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
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM