2017年3月17日 星期五

ios tableview 的row 設定




點選  table view ->   Row Height


tableview -> cell -> Row Height


新增加一個function

// 先取得 CGSize(width: 150.0, height: 150.0)

//   然後呼叫 UIGraphicsBeginImageContextWithOptions(imageSize, false0.0)

//  ...

func resizeCellImage(_ cell:UITableViewCell){
        let imageSize = CGSize(width: 150.0, height: 150.0)
        //  false ->  accept 秀明度
        UIGraphicsBeginImageContextWithOptions(imageSize, false, 0.0)
        let imageRect = CGRect(x: 0.0, y: 0.0, width: imageSize.width, height: imageSize.height)
        cell.imageView?.image?.draw(in: imageRect)
        cell.imageView?.image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

    }

最後 在     tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

裡面去呼叫剛剛的   resizeCellImage

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        // Configure the cell...
        //cell.textLabel?.text = "電影\(indexPath.row+1)"

        let movieCell = movies[indexPath.row]
        cell.textLabel?.text = movieCell.title
        cell.detailTextLabel?.text = movieCell.year
        
        downloadPoster(url: movieCell.poster!) { (data) in
            cell.imageView?.image = UIImage(data: data)
            self.resizeCellImage(cell)
            
        }
        
        
        return cell
    }

把圖片變成圓角

   cell.imageView?.layer.cornerRadius = imageSize.width/2

        cell.imageView?.layer.masksToBounds = true

  func resizeCellImage(_ cell:UITableViewCell){
        let imageSize = CGSize(width: 150.0, height: 150.0)
        //  false ->  accept 秀明度
        UIGraphicsBeginImageContextWithOptions(imageSize, false, 0.0)
        let imageRect = CGRect(x: 0.0, y: 0.0, width: imageSize.width, height: imageSize.height)
        cell.imageView?.image?.draw(in: imageRect)
        cell.imageView?.image = UIGraphicsGetImageFromCurrentImageContext()
        cell.imageView?.layer.cornerRadius = imageSize.width/2
        cell.imageView?.layer.masksToBounds = true
        UIGraphicsEndImageContext()

    }




沒有留言:

張貼留言