【问题标题】:Using CIImage initial report EXC_BAD_INSTRUCTION error in extension - swift在扩展中使用 CIImage 初始报告 EXC_BAD_INSTRUCTION 错误 - swift
【发布时间】:2016-12-01 04:45:20
【问题描述】:

在我问这个问题之前,我已经搜索了相关帖子:

"unrecognized selector" when attempting to access CIFilter's outputImage

我不知道是因为使用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

【问题讨论】:

    标签: ios swift ciimage


    【解决方案1】:

    我认为数据是nil

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

    另外,用CIImage 实例化的UIImage 没有位图,它没有实际图像,它只是一组应用过滤器的指令。所以你转换为 UIImage 的方法应该不起作用。

    【讨论】:

    • @Willian Hu,我测试过,不是因为data,请看我的EDIT。而我的网址不是http://prefix,是weixin://,有关系吗?
    • 尝试用NSISOLatin1StringEncoding替换utf8
    【解决方案2】:

    终于找到了一个过时的生成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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-08
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多