Scale image to fit - iOS -
i have uicollectionview displays 12 images. images not fitting in cells, being cropped, want them fit in cells without being cropped.
the code should make image scale fit is:
cell.imageview.contentmode = uiviewcontentmode.scaleaspectfit cell.imageview.image = uiimage(named: name)
here code whole viewcontroller class , screenshot:
import uikit class dressingroomviewcontroller: uiviewcontroller, uicollectionviewdelegateflowlayout, uicollectionviewdatasource { @iboutlet weak var collectionview: uicollectionview! let identifier = "cellidentifier" let datasource = datasource() override func viewdidload() { super.viewdidload() collectionview.datasource = self } override func viewdidappear(animated: bool) { // notes on equation cell size: // cells per row = 6 // cell spacing = 10 // collectionview.layout.inset = 20 (10 left, 10 right) let cellsize = (collectionview.collectionviewlayout .collectionviewcontentsize().width - 20) / 6 - 10 let layout: uicollectionviewflowlayout = uicollectionviewflowlayout() layout.sectioninset = uiedgeinsets( top: 20, left: 10, bottom: 10, right: 10) layout.itemsize = cgsize(width: cellsize, height: cellsize) collectionview.collectionviewlayout = layout } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } override func prepareforsegue( segue: uistoryboardsegue, sender: anyobject?) { if (segue.identifier == "dressingroom2myoutfits") { let myoutfitsviewcontroller = segue.destinationviewcontroller as! myoutfitsviewcontroller } } } // mark:- uicollectionviewdatasource delegate extension dressingroomviewcontroller : uicollectionviewdatasource { func numberofsectionsincollectionview( collectionview: uicollectionview) -> int { return 1 } func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int { return 12 } func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell { let cell = collectionview.dequeuereusablecellwithreuseidentifier( identifier,forindexpath:indexpath) as! fruitcell let fruits: [fruit] = datasource.fruits let fruit = fruits[indexpath.row] let name = fruit.name! cell.imageview.contentmode = uiviewcontentmode.scaleaspectfit cell.imageview.image = uiimage(named: name) return cell } }
edit: here fruitcell class, in case wondering.
class fruitcell: uicollectionviewcell { @iboutlet weak var imageview: uiimageview! }
a uiimageview has been placed in uicollectionviewcell interface builder, not have knowledge of uicollectionviewflowlayout has been initialised programmatically. layout.sectioninset had set, making uicollectionviewcells smaller, , when uiimageview created in interface builder constrained margins of uicollectionviewcell, uiimageview not resizing go smaller.
the solution initialise uiimageview programmatically , set size size of cell taking account cell spacing.
here code:
import uikit class dressingroomviewcontroller: uiviewcontroller, uicollectionviewdelegateflowlayout, uicollectionviewdatasource { @iboutlet weak var collectionview: uicollectionview! let identifier = "cellidentifier" let datasource = datasource() let layout: uicollectionviewflowlayout = uicollectionviewflowlayout() let cellspacing: cgfloat = 2 let cellsperrow: cgfloat = 6 override func viewdidload() { super.viewdidload() collectionview.datasource = self } override func viewdidappear(animated: bool) { let cellsize = (collectionview.collectionviewlayout .collectionviewcontentsize().width / cellsperrow) - (cellspacing) layout.itemsize = cgsize(width: cellsize, height: cellsize) layout.minimuminteritemspacing = cellspacing layout.minimumlinespacing = cellspacing collectionview.collectionviewlayout = layout } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } override func prepareforsegue( segue: uistoryboardsegue, sender: anyobject?) { if (segue.identifier == "dressingroom2myoutfits") { let myoutfitsviewcontroller = segue.destinationviewcontroller as! myoutfitsviewcontroller } } } // mark:- uicollectionviewdatasource delegate extension dressingroomviewcontroller : uicollectionviewdatasource { func numberofsectionsincollectionview( collectionview: uicollectionview) -> int { return 1 } func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int { return 12 } func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell { let cell = collectionview.dequeuereusablecellwithreuseidentifier( identifier,forindexpath:indexpath) as! fruitcell let fruits: [fruit] = datasource.fruits let fruit = fruits[indexpath.row] let name = fruit.name! var imageview :uiimageview imageview = uiimageview(frame:cgrectmake( 0, 0, (collectionview.collectionviewlayout .collectionviewcontentsize().width / cellsperrow) - (cellspacing), (collectionview.collectionviewlayout .collectionviewcontentsize().width / cellsperrow) - (cellspacing))) imageview.contentmode = uiviewcontentmode.scaleaspectfit imageview.image = uiimage(named: name) imageview.backgroundcolor = uicolor.redcolor() cell.addsubview(imageview) return cell } }
edit: able crop off empty space @ bottom of uicollectionview creating outlet height constraint ( control + drag height constraint in interface builder onto viewcontroller swift file) calling heightconstraint, , @ bottom of viewdidappear added these 2 lines of code:
self.heightconstraint.constant = collectionview.contentsize.height self.view.layoutifneeded()
so show result white background on uicollectionview , red background on uiimageviews, here result (also works on other device sizes):
Comments
Post a Comment