2014-08-28 191 views
3

我是一个初学者,而且我无法调整UIImageView的大小。如何调整UIImageView的大小(swift)

这是我目前的布局:

Current layout

我想是调整图像以占据屏幕的宽度的一半(每行2个图像)。

这是我的故事板:

storyboard

下面是函数绘制集合视图我的控制器内:

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 
    } 

任何线索来排序了这一点?

回答

3

您的单元格的大小由您的UICollectionViewLayout对象决定,这是您的案例中的UICollectionViewFlowLayout。如果您希望所有单元格的大小相同,则可以在布局本身上配置属性; itemSizeminimumInterimSpacing将会诀窍。另外,如果你希望你的细胞具有不同的尺寸,这取决于每一个包含图像或其他上说,你需要你的收藏视图委托实施UICollectionViewDelegateFlowLayout方法:

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

返回你想每个单元的大小。

+0

谢谢!现在我可以动态调整我的单元格大小。但我怎么能调整每个单元格内的图像? – 2014-09-03 14:01:47

+0

没问题,@AlexandreNucera。通过调整xib或代码中的各种自动布局约束,设置'UIImageView'的'autoresizingMask'属性或手动在'MenuInspirationCellView'的'layoutSubviews'函数中执行布局,可以调整每个单元格内的图像的大小。 – stefandouganhyde 2014-09-03 17:41:29

+0

谢谢,这正是我所需要的。我通过修改我的约束得到了这个工作(我删除了固定的宽度和高度)。 – 2014-09-04 09:56:27

0

在此之后教程,你可以调整的UIImage,在充分描述了教程.. 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 
} 

在这里还可以看到有关雨燕语言访问这里iPhone & iPad Application Development Help World

3

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 
} 
相关问题