生成你的 QRCode

Zhi-Hong Lin
2 min readJun 6, 2020

--

iOS 要實作出 QRCode 其實很簡單,這邊將透過 CIFilter 來產生

inputMessage

轉換成 QRCode 圖片的初始資料。此參數必須是 NSData

inputCorrectionLevel

表示有多少額外的錯誤更正資料要被附加到輸出的 QRCode 圖片。LMQH ,分別對應到不同的錯誤復原能力,依序為 7% 、 15% 、 25% 、 30% 。數值越大,輸出的 QR Code 圖片也就越大,預設值為 M

修正模糊圖片

上面產出的 CIImage 可能會有模糊不清的情況,使用 qrCodeImageoutputQRCodeImage 計算長寬的縮放比,再透過 transform 來達成縮放效果,如此一來 CIImage 就會是清晰的圖片了。

保存 QRCode 到相簿裡

如果你的 QRCode Image 是加在 UIView 裡的話,透過以下的 extension 將 view 轉成圖片

然後再將 view 存在相簿裡

let qrcodeImage = qrcodeView.convertToImage()UIImageWriteToSavedPhotosAlbum(qrcodeImage, nil, nil, nil)

官方文件的部分,有興趣可以深入研究

Sign up to discover human stories that deepen your understanding of the world.

--

--

No responses yet

Write a response