繁体   English   中英

调整UICollectionView单元格中图像的大小

[英]resize image in UICollectionView cell swift

我正在尝试使用UICollectionView以拼贴形式列出照片的集合。 问题是我现在遇到的是单元格中显示的照片放大了,只显示了其中的一部分。这是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{

}

我到底应该在哪里实现调整大小的功能? 它是在集合视图函数中还是在viewdidload中? 以及如何缩小照片?

这是图片

在此处输入图片说明

您的图像尺寸很大。 因此,您应该在ViewDiD加载中调整所有图像的大小,然后用调整大小的图像填充收藏夹视图。

使用此功能可以调整图像大小,并根据所需单元格的高度和宽度进行调整。

 + (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;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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