我有一个UIImageView
,它使用来自核心数据的低分辨率缩略图进行实例化。在显示视图后,将开始加载高分辨率Web版本,这将替换UIImageView
中的原始图像。但问题是,一旦高分辨率版本的下载完成,框架就会改变。这不会发生,因为原始图像顶部的模糊层将比原始图像的尺寸小。我使用以下代码:
let imageData = member.picture
if let imageData = imageData {
let image = imageFromData(imageData)
// Setup pictures
self.profilePicture.image = image
self.profilePictureBlurred.image = image
self.profilePictureBlurred.clipsToBounds = true
// Make profilePicture round
let width = self.profilePicture.frame.width
self.profilePicture.layer.cornerRadius = width * 0.5
self.profilePicture.layer.masksToBounds = true
self.profilePicture.layer.borderColor = UIColor.whiteColor().CGColor
self.profilePicture.layer.borderWidth = 2.0
// Blur profilePictureBlurred
// Create Effects
let blur = UIBlurEffect(style: .Light)
let vibrancy = UIVibrancyEffect(forBlurEffect: blur)
let effectView = UIVisualEffectView(effect: blur)
effectView.frame = self.profilePictureBlurred.frame
let vibrantView = UIVisualEffectView(effect: vibrancy)
vibrantView.frame = self.profilePictureBlurred.frame
print(self.profilePictureBlurred.frame)
// Add effects to profilePictureBlurred
self.profilePictureBlurred.addSubview(effectView)
self.profilePictureBlurred.addSubview(vibrantView)
let iPid = Int(member.iPid!)
getImageForPerson(iPid, completionHandler: { (image,error) in
if error == nil {
self.profilePicture.image = image
self.profilePictureBlurred.image = image
print(self.profilePictureBlurred.frame)
} else {
print("Error: \(error)")
}
})
} else {
print("No Image Data")
}
下载完成后,帧的大小会发生变化,printlog:
(0.0,0.0,600.0,150.0)
(0.0,0.0,375.0,167.0)
这意味着模糊效果不再覆盖整个UIImageView,从而导致难看的效果。为什么UIImageView会改变大小以及第二次在UIImageView中设置图像的正确方法?
目标是创建类似LinkedIn contact view
的内容编辑:出口:
@IBOutlet weak var profilePictureBlurred: UIImageView!
@IBOutlet weak var profilePicture: UIImageView!