I am trying to use UICollectionView to list a collection of photo in tiles form. The problem is I'm encountering now is that the photo showed in the cell looks zoomed in and show only the part of it.Here is the code in viewcontroller:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
var imageArray = [UIImage(named: "cake1")!, UIImage(named: "cake2")!, UIImage(named: "cake3")!]
var imageTitle:[String] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController : UICollectionViewDataSource{
//return number of the cake image
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return self.imageArray.count
}
//return the specific cake image
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cakeCell", forIndexPath: indexPath) as! CakeCell
cell.cakeImage?.image = self.imageArray[indexPath.row]
return cell
}
//action when the image is clicked
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
self.performSegueWithIdentifier("showCake", sender: self)
}
//prepare for segue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showCake"{
if let indexPaths = self.collectionView!.indexPathsForSelectedItems(){
let indexPath = indexPaths[0] as NSIndexPath
let vc = segue.destinationViewController as! CakeDetailViewController
vc.image = self.imageArray[indexPath.row]
}
}
}
}
extension ViewController : UICollectionViewDelegate{
}
where would exactly should I implement the resizing function? is it inside the collection view function or at viewdidload? and how should I zoom out the photo?
Here is the image
Your Image Size Is Quite Large . So you should resize all your Images in ViewDiD load and then fill the collection view with resized images.
Use This Function to resize your images and resize according to the height and width of cell you required.
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
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.