Dans mon application, il existe deux options de flou et de flou d'image en touchant l'image. Je fais cela pour brouiller l'image lorsque l'utilisateur touche l'image
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
var croppedImg: UIImage? = nil
let touch = touches.first
var currentPoint = touch!.location(in: imgBackground)
let ratioW: Double = Double(imgBackground.image!.size.width / imgBackground.frame.size.width)
let ratioH: Double = Double(imgBackground.image!.size.height / imgBackground.frame.size.height)
currentPoint.x *= CGFloat(ratioW)
currentPoint.y *= CGFloat(ratioH)
let circleSizeW = 25 * ratioW
let circleSizeH = 25 * ratioH
currentPoint.x = CGFloat((Double(currentPoint.x ) - circleSizeW / 2 < 0)? 0: Double(currentPoint.x ) - circleSizeW / 2)
currentPoint.y = CGFloat((Double(currentPoint.y ) - circleSizeH / 2 < 0)? 0: Double(currentPoint.y ) - circleSizeH / 2)
let cropRect = CGRect(x: currentPoint.x, y: currentPoint.y, width: CGFloat(circleSizeW), height: CGFloat(circleSizeH))
croppedImg = croppIngimage(byImageName: imgBackground?.image, to: cropRect)
// Blur Effect
croppedImg = croppedImg?.imageWithGaussianBlur9()
croppedImg = roundedRectImage(from: croppedImg, withRadious: 4)
imgBackground.image = addImage(to: imgBackground.image, withImage2: croppedImg, andRect: cropRect)
}
func imageWithGaussianBlur9() -> UIImage? {
// Blur horizontally
UIGraphicsBeginImageContextWithOptions(size, _: false, _: scale)
draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height), blendMode:.normal, alpha: 0.5)
for x in 1..<5 {
draw(in: CGRect(x: CGFloat(x), y: 0, width: size.width, height: size.height), blendMode:.normal, alpha: 0.5)
draw(in: CGRect(x: CGFloat(-x), y: 0, width: size.width, height: size.height), blendMode:.normal, alpha: 0.5)
}
let horizBlurredImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
// Blur vertically
UIGraphicsBeginImageContextWithOptions(size, _: false, _: scale)
horizBlurredImage?.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height), blendMode:.normal, alpha: 0.5)
for y in 1..<5 {
horizBlurredImage?.draw(in: CGRect(x: 0, y: CGFloat(y), width: size.width, height: size.height), blendMode:.normal, alpha: 0.5)
horizBlurredImage?.draw(in: CGRect(x: 0, y: CGFloat(-y), width: size.width, height: size.height), blendMode:.normal, alpha: 0.5)
}
let blurredImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//
return blurredImage
}
Le code ci-dessus fonctionne parfaitement lorsque nous floutons l'image
Que dois-je faire lorsque l'utilisateur souhaite supprimer l'effet de flou
Solution du problème
vous devez placer 2 UIImageView l'un sur l'autre. En bas, vous devez placer l'image originale.
@IBOutlet weak var imgBackground:UIImageView!
@IBOutlet weak var bottomViewImage:UIImageView! // original image
Add below code for unblur the image -:
var croppedImg: UIImage? = nil
let touch = touches.first
var currentPoint = touch!.location(in: imgBackground)
let ratioW: Double = Double(imgBackground.image!.size.width / imgBackground.frame.size.width)
let ratioH: Double = Double(imgBackground.image!.size.height / imgBackground.frame.size.height)
currentPoint.x *= CGFloat(ratioW)
currentPoint.y *= CGFloat(ratioH)
let circleSizeW = 25 * ratioW
let circleSizeH = 25 * ratioH
currentPoint.x = CGFloat((Double(currentPoint.x ) - circleSizeW / 2 < 0)? 0: Double(currentPoint.x ) - circleSizeW / 2)
currentPoint.y = CGFloat((Double(currentPoint.y ) - circleSizeH / 2 < 0)? 0: Double(currentPoint.y ) - circleSizeH / 2)
let cropRect = CGRect(x: currentPoint.x, y: currentPoint.y, width: CGFloat(circleSizeW), height: CGFloat(circleSizeH))
croppedImg = croppIngimage(byImageName: bottomViewImage?.image, to: cropRect)
croppedImg = roundedRectImage(from: croppedImg, withRadious: 4)
imgBackground.image = addImage(to: imgBackground.image, withImage2: croppedImg, andRect: cropRect)
Aucun commentaire:
Enregistrer un commentaire