2016-12-16 14 views

答えて

0

:私は何を達成したいことは、それはそれは私がこのようにそれを持っているdynamically.Currently含まれた画像に応じたImageViewのサイズを変更する必要があります。

最初に、画像コンテンツモードをAspect Fit BUTに設定するだけで十分ではありません。

新しい画像を読み込むたびに、imageViewのアスペクト比の制約を変更する必要があります。基本的に、何をする必要がある:

  1. あなたのイメージの幅と高さ
  2. 計算アスペクト比let aspectRatio = Float(image.height)/Float(image.width)
  3. があなたのイメージ図のための新たなアスペクト比の制約を作成するために、そのアスペクト比を使用して取得します。

これは私がこの問題を管理していたコードのコピー貼り付けです(コメントが追加されています)。それがあなたを助けることを願っています。

// aspectRatioCnst is IBOutlet reference to aspect ratio constraint I've set on mainImageView in Storyboard 
    if let aspectRatioCnst = aspectRatioCnst { 
     aspectRatioCnst.isActive = false 
    } 
    let aspectRatio = Float(imagePost.height)/Float(imagePost.width) 
    aspectRatioCnst = NSLayoutConstraint(
     item: self.mainImageView, 
     attribute: NSLayoutAttribute.height, 
     relatedBy: NSLayoutRelation.equal, 
     toItem: self.mainImageView, 
     attribute: NSLayoutAttribute.width, 
     multiplier: CGFloat(aspectRatio), 
     constant: 0) 
    if let aspectRatioCnst = aspectRatioCnst { 
     self.mainImageView.addConstraint(aspectRatioCnst) 
     aspectRatioCnst.isActive = true 
     self.mainImageView.layoutIfNeeded() 
    } 
    if let image = imagePost.loadedImage { 
     self.mainImageView.image = image 

    } else if let imageURL = URL(string: imagePost.fileURL) { 
     DispatchQueue.global(qos: .background).async { 
      // UIImage extension with downloadedFromURL method 
      UIImage.downloadedFromURL(url: imageURL, withCallback: { (image) -> (Void) in 
       DispatchQueue.main.async { 
        self.imageLoadingActivityIndicator.stopAnimating() 
        self.mainImageView.image = image 
        self.imagePost?.loadedImage = image 
       } 
      }) 
     } 
    } 
+0

は何の画像が非同期にダウンロードしている場合、このような気にいらを取得する必要があります。それでは、コードがイメージの高さと幅をどのように知るのですか? –

+0

それは良い点です。私もその部分をコピーする必要はないと思ったが、あなたが尋ねたので、私は自分の答えを更新した。 P.S.画像URLをポイントしているレスポンスJSONでは、画像の高さと幅が取得されるため、計算する必要はありません。 – JPetric

+0

基本的には、最初に、すべての画像の高さと幅、および画像をダウンロードできるURLがあるすべての画像メタデータをダウンロードします。そして、私がテーブルビューのセルをデキューすると、私はただ非同期にイメージをダウンロードします。私はそれを少し明確にしたいと思っています。 – JPetric

0

まず、あなたのコードでこれを行うその後

enter image description here

、あなたのイメージは次のように制約を設定すると

enter image description here

、あなたの下のコンテンツこのような高さを設定

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 

     let imageNew = UIImage(named: "test") //Set your image here 
     let oldWidth = imageNew!.size.width 
     let scaleFactor = tableView.frame.size.width/oldWidth 

     let newHeight = imageNew!.size.height * scaleFactor 
     let newWidth = oldWidth * scaleFactor 

     //Finally to get cell size just add the bottom part height for othercontents to ImageHeight here 
     let CellSize = CGSize(width: newWidth, height: (newHeight + 40)) 
     return CellSize.height 

    } 


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell 
     cell.NewImage.image = UIImage(named: "test") 
     return cell 

    } 

あなたは enter image description here

関連する問題