2017-04-19 2 views
2

hereUIImageView 하위 클래스를 사용하여 비동기 적으로 다운로드 한 이미지를 UITableViewCell 안에 설정하고 올바른 가로 세로 비율을 유지하고 있습니다. 그 클래스는 다음과 같습니다AlamofireImage를 사용하여 올바른 치수로 이미지를 설정하는 데 문제가 있음

internal final class ScaleAspectFitImageView: UIImageView { 

    /// Constraint to maintain same aspect ratio as the image. 
    private var aspectRatioConstraint: NSLayoutConstraint? = nil 

    required public init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     setup() 
    } 

    public override init(frame:CGRect) { 
     super.init(frame: frame) 
     setup() 
    } 

    public override init(image: UIImage!) { 
     super.init(image: image) 
     setup() 
    } 

    public override init(image: UIImage!, highlightedImage: UIImage?) { 
     super.init(image: image, highlightedImage: highlightedImage) 
     setup() 
    } 

    override public var image: UIImage? { 
     didSet { 
      print("\nUpdating aspect ratio constraints") 
      updateAspectRatioConstraint() 
      print("Updated aspect ratio constraints\n") 
     } 
    } 

    private func setup() { 
     contentMode = .scaleAspectFit 
     updateAspectRatioConstraint() 
    } 

    /// Removes any pre-existing aspect ratio constraint, and adds a new one based on the current image 
    private func updateAspectRatioConstraint() { 
     // Remove any existing aspect ratio constraint 
     if let constraint = aspectRatioConstraint { 
      removeConstraint(constraint) 
     } 
     aspectRatioConstraint = nil 
     if let imageSize = image?.size, imageSize.height != 0 { 
      let aspectRatio = imageSize.width/imageSize.height 
      let constraint = NSLayoutConstraint(item: self, attribute: .width, relatedBy: .equal, toItem: self, attribute: .height, multiplier: aspectRatio, constant: 0) 
      addConstraint(constraint) 
      aspectRatioConstraint = constraint 
     } 
    } 

} 

가 나는 또한 다음과 tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)의 내부 이미지 다운로드 AlamofireImage을 사용하고 있습니다 : 이미지를 다운로드 할 때

그러나
cell.embeddedImageImageView.layer.borderColor = UIColor.red.cgColor 
cell.embeddedImageImageView.layer.borderWidth = 2 

let placeholderImage = #imageLiteral(resourceName: "postImagePlaceholder") 

if let embeddedImageURL = message.mediaURL { 
    let filter = AspectScaledToFitSizeFilter(size: cell.embeddedImageImageView.frame.size) 
    cell.embeddedImageImageView.af_setImage(withURL: embeddedImageURL, placeholderImage: placeholderImage, filter: filter) 
} else { 
    cell.embeddedImageImageView.image = placeholderImage 
} 

, 그들이 셀의 embeddedImageImageView의 내부에 배치됩니다, 크기가 잘못되었습니다.

다음은 ScaleAspectFitImageView 서브 클래스 인 UIImageView 주위에 빨간색 테두리를 배치 한 내 셀의 스크린 샷입니다.

Screenshot of current image dimensions

내가 잘못 뭐하는 거지 그 적당한 크기로 수 없습니다하는 내 이미지를 일으키는?

답변

0

세포의 이미지보기를 ScaleAspectFitImageView의 하위 클래스로 만들었습니까?

enter image description here

서브 클래스를 실행하면 원인 EXC_BAD_ACCESS을 기록했다. 그래서 aspectRatioConstraint 속성과 그 사용법을 삭제하고 코드를 실행했습니다. 모든 것이 완벽하게 작동합니다.

관련 문제