给UIImage添加Extension
import UIKit
extension UIImage {
///生成二维码
fileprivate class func CIImageCreat(codeString:String) -> CIImage?{
let filter = CIFilter.init(name: "CIQRCodeGenerator");
filter?.setDefaults();
let dataString = codeString.data(using: String.Encoding.utf8);
filter?.setValue(dataString, forKey: "inputMessage");
let outImage = filter?.outputImage;
return outImage;
}
///绘制二维码高清图
fileprivate class func QR_HUD_image(outImage:CIImage,sizes:CGFloat) -> UIImage {
let extent = outImage.extent.integral;
let scale = min(sizes/extent.width, sizes/extent.height)
///创建bitmap
let with = extent.width * scale;
let heigh = extent.height * scale;
///颜色通道
let colorSpaceRef = CGColorSpaceCreateDeviceGray()
let imageAlphaInfo = CGImageAlphaInfo.none.rawValue
let bitmap = CGContext.init(data: nil, width: Int(with), height: Int(heigh), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpaceRef, bitmapInfo: imageAlphaInfo);
let context = CIContext.init();
let bitmapImage = context.createCGImage(outImage, from: extent)
bitmap?.interpolationQuality = CGInterpolationQuality.none;
bitmap?.scaleBy(x: scale, y: scale);
bitmap?.draw(bitmapImage! , in: extent);
let scalImage = bitmap?.makeImage();
return UIImage.init(cgImage: scalImage!);
}
class func creatQRImage(codeString:String,sizes:CGFloat) -> UIImage {
let outImage = CIImageCreat(codeString: codeString);
let image = QR_HUD_image(outImage: outImage!, sizes: sizes)
return image;
}
}
保存至相册
有3中方法,这里只写一种
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
网友评论