2016-12-08 6 views
0

私のアプリはバックエンドから画像を読み込んでUITableViewCellに表示します。UITableViewCellにはUIImageViewとそのラベルやボタンが表示されます。UIImageはラベルが.scaleAspectFillに設定されていると重複します

「提案された制約にリセットする」オプションを使用して、提案された制約をUITableViewCellに追加しました。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    var cell = PostTableViewCell() 

    if (self.posts.count == 0) { return cell } 

    let post = posts[indexPath.row] 

    // Instancia o reuse identifier 
    if post["post_image"] != nil { 
     cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.PostWithImage, for: indexPath) as! PostTableViewCell 
    } else { 
     cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.PostWithoutImage, for: indexPath) as! PostTableViewCell 
    } 
    return cell 
} 


override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    var cell = PostTableViewCell() 
    cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.PostWithImage) as! PostTableViewCell 
    return cell.bounds.size.height; 
} 


override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    var cell = PostTableViewCell() 
    cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.PostWithImage) as! PostTableViewCell 
    return cell.bounds.size.height; 
} 

private func configureCell(cell: PostTableViewCell, atIndexPath indexPath: IndexPath) { 

    cell.queue.cancelAllOperations() 

    let operation: BlockOperation = BlockOperation() 
    operation.addExecutionBlock { [weak operation]() -> Void in 

     DispatchQueue.main.sync(execute: { [weak operation]() -> Void in 
      if (operation?.isCancelled)! { return } 

      let post = self.posts[indexPath.row] 
      cell.accessibilityIdentifier = post.recordID.recordName 

      guard let postTitle = post["post_title"], let postBody = post["post_body"] else { 
       return 
      } 

      if let asset = post["post_image"] as? CKAsset { 

       self.imageCache.queryDiskCache(forKey: post.recordID.recordName, done: { (image, cachetype) in 
        if image != nil { 
         cell.postImageView.contentMode = .scaleAspectFill 
         cell.postImageView.autoresizingMask = [.flexibleBottomMargin, 
                   .flexibleHeight, 
                   .flexibleLeftMargin, 
                   .flexibleRightMargin, 
                   .flexibleTopMargin, 
                   .flexibleWidth ]; 
         cell.postImageView.image = image! 
        } else { 
         do { 
          let data = try Data(contentsOf: asset.fileURL) 
          let image = UIImage(data: data) 
          cell.postImageView.contentMode = .scaleAspectFill 
          cell.postImageView.autoresizingMask = [.flexibleBottomMargin, 
                    .flexibleHeight, 
                    .flexibleLeftMargin, 
                    .flexibleRightMargin, 
                    .flexibleTopMargin, 
                    .flexibleWidth ]; 
          cell.postImageView.image = image! 
          self.imageCache.store(image!, forKey: post.recordID.recordName) 

         } catch { 
          print("Error 1001 = \(error.localizedDescription)") 
         } 
        } 
       }) 

      } 

      cell.titleLabel.text = postTitle as? String 
      cell.bodyLabel.text = postBody as? String 
     }) 
    } 
    cell.queue.addOperation(operation) 
} 

Here'sラベルの上に画像の重なりを示しアプリ自体からいくつかの版画:ここ

は、データを取得した後、コードの一部です。

画像がポートレートモードの場合にのみ重なり合います。画像が横長で撮影された場合、その画像はうまく収まります。

この問題を回避する最適な方法は何ですか?

+0

.scaleAspectFillに設定されていない場合、ラベルは重複しませんが、正しくフィットしません。 –

+0

imageViewのclipsToBounds = trueを設定しましたか? – Aakash

答えて

2

指定された画像領域にのみ描画するようにプログラムによって指示することができます。制約が適切に働いていて、正しいサイズのままである場合、画像の場合は、.scaleAscpedtFill設定のためにビュー境界を超えて描画されます。

これを行うには、.clipToBounds = trueを使用します。

cell.postImageView.clipToBounds = true 

また、以下の画像ごとにインタフェースビルダーで設定することもできます。

Enable Clip To Bounds

は試してみて、それが助けかどうかを確認することを与えますか?

+0

Hmm。私は試してみよう、私はフィードバックinm –

+0

まあ、あなたの私の問題を解決したことを教えてください。ブライアン、ありがとうございました。 –

+0

驚くばかり!それがうれしかった。 –

関連する問題