使用CIIMAGE初始报告EXC_BAD_Instruction误差扩展 - Swift



在我问这个问题之前,我搜索了相关文章:

"未识别的选择器"尝试访问Cifilter的输出图

我不知道是因为使用swift还是extension,我会得到error。我已经测试了两个methods以获取CIImage,但在EXC_BAD_INSTRUCTION中失败:

注意
我的url不是http://前缀,而是weixin://wxpay/bizpayurl?pr=ZwBVaW0,我认为这不是错误的原因。

  1. 方法一:

    扩展字符串{

     func initQRImage() ->UIImage {
         let filter:CIFilter = CIFilter.init(name: "CIQRCodeGenerator")!
         filter.setDefaults()
         let data:Data = self.data(using: String.Encoding.utf8)!
         filter.setValue(data, forKey: "inputMessage")
         let outputImage:CGImage = filter.outputImage as! CGImage // EXC_BAD_INSTRUCTION here
         let qr_image = UIImage.init(cgImage: outputImage)
         return qr_image
     }
    

    }

  2. 方法两个:

    扩展字符串{

     func initQRImage() ->UIImage {
         let url:URL = URL.init(fileURLWithPath: self)
         let inputImage:CIImage = CIImage.init(contentsOf: url)!  // EXC_BAD_INSTRUCTION here
         let filter: CIFilter = CIFilter.init(name: "CIAreaAverage")!
         filter.setValue(inputImage, forKey: kCIInputImageKey)
         let inputExtent:CGRect = inputImage.extent
         let extent:CIVector = CIVector.init(x: inputExtent.origin.x, y: inputExtent.origin.y, z: inputExtent.size.width, w: inputExtent.size.height)
         filter.setValue(extent, forKey: kCIInputExtentKey)
         let outputImage:CIImage = filter.value(forKey: "outputImage") as! CIImage
         let qr_image = UIImage.init(cgImage: outputImage as! CGImage)
         return qr_image
     }
    

    }

两种方法将在此处报告EXC_BAD_INSTRUCTION错误,您可以在报告错误行后看到我写的annotation


编辑-1

我再次尝试了我的项目,不使用extension,也有error,而data不是nil

我认为数据是nil

let data:Data = self.data(using: String.Encoding.utf8)!

也是通过CIImage实例化的UIImage没有位图,没有实际图像,它只是应用过滤器的一组指令。因此,您转换为UIIMAGE的方法不应工作。

最后,我找到了一种过时的方法来生成QR code,在我改进之后,它变成了:

// quality can modify the defintion
class func generateQRImage(stringQR:NSString, withSizeRate rate:CGFloat, quality:CGFloat?) -> UIImage
{
    let filter:CIFilter = CIFilter(name:"CIQRCodeGenerator")!
    filter.setDefaults()
    let data:NSData = stringQR.data(using: String.Encoding.utf8.rawValue)! as NSData
    filter.setValue(data, forKey: "inputMessage")
    let outputImg:CIImage = filter.outputImage!
    let context:CIContext = CIContext(options: nil)
    var tmp_quality = quality
    if quality == nil {
        tmp_quality = 1.0
    }
    let transform: CGAffineTransform  = CGAffineTransform(scaleX: tmp_quality!, y: tmp_quality!);
    let outputImg_after = outputImg.applying(transform)
    let cgimg:CGImage = context.createCGImage(outputImg_after, from: outputImg_after.extent)!
    var img:UIImage = UIImage(cgImage: cgimg, scale: 1.0, orientation: UIImageOrientation.up)
    let width  = img.size.width * rate
    let height = img.size.height * rate
    UIGraphicsBeginImageContext(CGSize.init(width: width, height: height))
    let cgContxt:CGContext = UIGraphicsGetCurrentContext()!
    cgContxt.interpolationQuality = .high // cgContxt kCGInterpolationNone
    img.draw(in: CGRect.init(x: 0, y: 0, width: width, height: height))  // (0, 0, width, height)
    img = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    return img
}

相关内容

  • 没有找到相关文章

最新更新