簡體   English   中英

如何調整UIImageView(swift)的大小

[英]How to resize an UIImageView (swift)

我是swift的初學者,我無法調整UIImageView的大小。

這是我目前的布局:

目前的布局

我想要的是調整圖像大小占據屏幕寬度的一半(每行2個圖像)。

這是我的故事板:

故事板

這是在我的控制器中繪制集合視圖的函數:

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
    }

任何導致排除這一點?

單元格的大小由您的UICollectionViewLayout對象決定,在您的情況下,這是一個UICollectionViewFlowLayout 如果您希望所有單元格大小相同,則可以在布局本身上配置屬性; itemSizeminimumInterimSpacing可以解決這個問題。 或者,如果您希望單元格的大小不同,例如,根據每個包含的圖像或其他內容,您需要集合視圖委托來實現UICollectionViewDelegateFlowLayout方法:

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

返回每個單元格所需的大小。

迅速:

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
}

按照本教程,您可以調整UIImage的大小,在完全描述的教程中。 圖像在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
}

在這里,您還可以看到許多關於Swift語言的教程訪問此處iPhone和iPad應用程序開發幫助世界

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM