How to remove the border/drop shadow from an UIIma

2019-08-10 14:32发布

问题:

I've been generating QR Codes using the CIQRCodeGenerator CIFilter and it works very well:

But when I resize the UIImageView and generate again

  @IBAction func sizeSliderValueChanged(_ sender: UISlider) {
    qrImageView.transform = CGAffineTransform(scaleX: CGFloat(sender.value), y: CGFloat(sender.value))
  }

I get a weird Border/DropShadow around the image sometimes:

How can I prevent it from appearing at all times or remove it altogether?

I have no idea what it is exactly, a border, a dropShadow or a Mask, as I'm new to Swift/iOS.

Thanks in advance!

PS. I didn't post any of the QR-Code generating code as it's pretty boilerplate and can be found in many tutorials out there, but let me know if you need it

EDIT: code to generate the QR Code Image

private func generateQRCode(from string: String) -> UIImage? {
    let data = string.data(using: String.Encoding.ascii)

    guard let filter = CIFilter(name: "CIQRCodeGenerator") else {
      return nil
    }

    filter.setValue(data, forKey: "inputMessage") 

    guard let qrEncodedImage = filter.outputImage else {
      return nil
    }

    let scaleX = qrImageView.frame.size.width / qrEncodedImage.extent.size.width
    let scaleY = qrImageView.frame.size.height / qrEncodedImage.extent.size.height
    let transform = CGAffineTransform(scaleX: scaleX, y: scaleY )

    if let outputImage = filter.outputImage?.applying(transform) {
      return UIImage(ciImage: outputImage)
    }

    return nil
  }

Code for button pressed

  @IBAction func generateCodeButtonPressed(_ sender: CustomButton) {
    if codeTextField.text == "" {
      return
    }

    let newEncodedMessage = codeTextField.text!
    let encodedImage: UIImage = generateQRCode(from: newEncodedMessage)!
    qrImageView.image = encodedImage
    qrImageView.transform = CGAffineTransform(scaleX: CGFloat(sizeSlider.value), y: CGFloat(sizeSlider.value))


    qrImageView.layer.minificationFilter = kCAFilterNearest
    qrImageView.layer.magnificationFilter = kCAFilterNearest
  }

回答1:

It’s a little hard to be sure without the code you’re using to generate the image for the image view, but that looks like a resizing artifact—the CIImage may be black or transparent outside the edges of the QR code, and when the image view size doesn’t match the image’s intended size, the edges get fuzzy and either the image-outside-its-boundaries or the image view’s background color start bleeding in. Might be able to fix it by setting the image view layer’s minification/magnification filters to “nearest neighbor”, like so:

imageView.layer.minificationFilter = kCAFilterNearest
imageView.layer.magnificationFilter = kCAFilterNearest

Update from seeing the code you added—you’re currently resizing the image twice, first with the call to applying(transform) and then by setting a transform on the image view itself. I suspect the first resize is adding the blurriness, which the minification / magnification filter I suggested earlier then can’t fix. Try shortening generateQRCode to this:

private func generateQRCode(from string: String) -> UIImage? {
    let data = string.data(using: String.Encoding.ascii)

    guard let filter = CIFilter(name: "CIQRCodeGenerator") else {
        return nil
    }

    filter.setValue(data, forKey: "inputMessage")

    if let qrEncodedImage = filter.outputImage {
        return UIImage(cgImage: qrEncodedImage)
    }

    return nil
}


回答2:

I think the problem here is that you try to resize it to "non-square" (as your scaleX isn't always the same as scaleY), while the QR code is always square so both side should have the same scale factor to get a non-blurred image.

Something like:

let scaleX = qrImageView.frame.size.width / qrEncodedImage.extent.size.width
let scaleY = qrImageView.frame.size.height / qrEncodedImage.extent.size.height
let scale = max(scaleX, scaleY)
let transform = CGAffineTransform(scaleX: scale, y: scale)

will make sure you have "non-bordered/non-blurred/squared" UIImage.



回答3:

I guess the issue is with the image(png) file not with your UIImageView. Try to use another image and I hope it will work!