我在UIImageView中出现了动态扭曲的UIImage(基本上用函数更新了UIImageView的图像)。每次我根据失真和触摸响应尝试裁剪图像时,我都会收到一个致命的错误 - 尝试用nil展开一个可选的回复。
有什么想法吗?
func pincushionDistortionWithAmount(amount:CGFloat, position:CGPoint){
//1 Remove All Subviews
for view in subviews {
view.removeFromSuperview()
}
//2
let beginImage = CIImage(image: UIImageView.image)
// 3
let filter :CIFilter = CIFilter(name: "CIBumpDistortion")!
filter.setValue(beginImage, forKey: kCIInputImageKey)
filter.setValue(CIVector(x: position.x, y: position.y), forKey:"inputCenter")
filter.setValue(75, forKey:"inputRadius")
filter.setValue(-1.0 * amount, forKey:"inputScale")
// 4
let newImage:UIImage = UIImage(CIImage: filter.outputImage!)
let imageViewWidth = pinImageView.frame.size.width
let imageViewHeight = pinImageView.frame.size.height
let imageWidth = newImage.size.width
let imageHeight = newImage.size.height
if imageWidth > imageViewWidth{
print("Image is wider than ImageView")
//Code to crop image to fit in imageView
}
if imageHeight > imageViewHeight{
print("Image is taller than ImageView")
//Code to crop image to fit in imageView
}
UIImageView.image = newImage
addSubview(UIImageView)
}