//MARK: -传进去字符串,生成二维码图片
private func creatQRCodeImage(text: String) -> UIImage{
//创建滤镜
let filter = CIFilter(name: "CiqrCodeGenerator")
//还原滤镜的默认属性
filter?.setDefaults()
//设置需要生成二维码的数据
filter?.setValue(text.dataUsingEnCoding(NSUTF8StringEnCoding),forKey: "inputMessage")
//从滤镜中取出生成的图片
let ciImage = filter?.outputimage
//这个清晰度不好
//let bgImage = UIImage(CIImage: ciImage!)
//这个清晰度好
let bgImage = createNonInterpolatedUIImageFormCIImage(ciImage!,size: 300)
//创建一个头像
let icon = UIImage(named: "navigationbar_pop_highlighted")
//合成图片(把二维码和头像合并)
let newImage = creatimage(bgImage,iconImage: icon!)
//返回生成好的二维码
return newImage
}
//MARK: - 根据背景图片和头像合成头像二维码
private func creatimage(bgImage: UIImage,iconImage:UIImage) -> UIImage{
//开启图片上下文
UIGraphicsBeginImageContext(bgImage.size)
//绘制背景图片
bgImage.drawInRect(CGRect(origin: CGPointZero,size: bgImage.size))
//绘制头像
let wIDth: CGfloat = 50
let height: CGfloat = wIDth
let x = (bgImage.size.wIDth - wIDth) * 0.5
let y = (bgImage.size.height - height) * 0.5
iconImage.drawInRect(CGRect(x: x,y: y,wIDth: wIDth,height: height))
//取出绘制好的图片
let newImage = UIGraphicsGetimageFromCurrentimageContext()
//关闭上下文
UIGraphicsEndImageContext()
//返回合成好的图片
return newImage
}
//MARK: -根据CIImage生成指定大小的高清UIImage
private func createNonInterpolatedUIImageFormCIImage(image: CIImage,size: CGfloat) -> UIImage {
let extent: CGRect = CGRectIntegral(image.extent)
let scale: CGfloat = min(size/CGRectGetWIDth(extent),size/CGRectGetHeight(extent))
let wIDth = CGRectGetWIDth(extent) * scale
let height = CGRectGetHeight(extent) * scale
let cs: CGcolorSpaceRef = CGcolorSpaceCreateDeviceGray()!
let bitmapRef = CGBitmapContextCreate(nil,Int(wIDth),Int(height),8,cs,0)!
let context = CIContext(options: nil)
let bitmAPImage: CGImageRef = context.createCGImage(image,fromrect: extent)
CGContextSetInterpolationQuality(bitmapRef,CGInterpolationQuality.None)
CGContextScaleCTM(bitmapRef,scale,scale);
CGContextDrawImage(bitmapRef,extent,bitmAPImage);
let scaledImage: CGImageRef = CGBitmapContextCreateImage(bitmapRef)!
return UIImage(CGImage: scaledImage)
}
总结以上是内存溢出为你收集整理的swift系统自带二维码的生成全部内容,希望文章能够帮你解决swift系统自带二维码的生成所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)