You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
3.1 KiB
88 lines
3.1 KiB
1 year ago
|
//
|
||
1 year ago
|
// WeChatSportViewModelss.swift
|
||
1 year ago
|
// FireBoltt
|
||
1 year ago
|
//
|
||
|
// Created by lemo. on 2020/10/27.
|
||
|
// Copyright © 2020 Sheldon. All rights reserved.
|
||
|
//
|
||
|
|
||
|
import UIKit
|
||
|
import RxSwift
|
||
|
|
||
1 year ago
|
class WeChatSportViewModelss: ViewModel, ViewModelType {
|
||
1 year ago
|
|
||
|
struct Input {
|
||
|
let saveQrCode: Observable<Void>
|
||
|
}
|
||
|
|
||
|
struct Output {
|
||
|
let qrCodeImage: BehaviorRelay<UIImage?>
|
||
|
|
||
|
}
|
||
|
|
||
|
func transform(input: Input) -> Output {
|
||
|
// 获取二维码
|
||
|
let qrCodeImage = BehaviorRelay<UIImage?>(value: nil)
|
||
1 year ago
|
if let deviceInfo = UserDefaultsManagerFrieBoltt.getDeviceInfo() {
|
||
1 year ago
|
SVProgressHUD.show()
|
||
|
ProviderRequest(.wechatGetCode(deviceMac: deviceInfo.MACString))
|
||
|
.subscribe(onNext: { [weak self] json in
|
||
|
SVProgressHUD.dismiss()
|
||
|
if let url = json["data"].string {
|
||
|
let image = self?.creatQrcode(url: url)
|
||
|
qrCodeImage.accept(image)
|
||
|
}
|
||
|
})
|
||
|
.disposed(by: rx.disposeBag)
|
||
|
}
|
||
|
// 保存到相册
|
||
|
input.saveQrCode
|
||
|
.subscribe(onNext: { [weak self] _ in
|
||
|
if let image = qrCodeImage.value {
|
||
1 year ago
|
SystemAuthorityFireBoltt.shared.getPhotoAuthority { [weak self] (result) in
|
||
1 year ago
|
if result {
|
||
|
// 保存图片至相册
|
||
|
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil)
|
||
1 year ago
|
SVProgressHUD.showSuccess(withStatus: MultiLanguageKey_FB.saveQrCodeSuccessFB.localized)
|
||
1 year ago
|
}
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
.disposed(by: rx.disposeBag)
|
||
|
|
||
|
return Output(qrCodeImage: qrCodeImage)
|
||
|
}
|
||
|
|
||
|
private func creatQrcode(url: String) -> UIImage {
|
||
|
/// CIFilter
|
||
|
let filter = CIFilter.init(name: "CIQRCodeGenerator")
|
||
|
filter?.setDefaults()
|
||
|
/// Add Data
|
||
|
let data = url.data(using: .utf8)
|
||
|
filter?.setValue(data, forKeyPath: "inputMessage")
|
||
|
/// Out Put
|
||
|
let outputImage = filter?.outputImage
|
||
|
/// Show QRCode
|
||
1 year ago
|
return createUIImageFromCIImages(image: outputImage!, size: 200)
|
||
1 year ago
|
}
|
||
|
|
||
|
// 高清处理
|
||
1 year ago
|
private func createUIImageFromCIImages(image: CIImage, size: CGFloat) -> UIImage {
|
||
1 year ago
|
let extent = image.extent.integral
|
||
|
let scale = min(size / extent.width, size / extent.height)
|
||
|
/// Create bitmap
|
||
|
let width: size_t = size_t(extent.width * scale)
|
||
|
let height: size_t = size_t(extent.height * scale)
|
||
|
let cs: CGColorSpace = CGColorSpaceCreateDeviceGray()
|
||
|
let bitmap: CGContext = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: cs, bitmapInfo: 1)!
|
||
|
let context = CIContext.init()
|
||
|
let bitmapImage = context.createCGImage(image, from: extent)
|
||
|
bitmap.interpolationQuality = .none
|
||
|
bitmap.scaleBy(x: scale, y: scale)
|
||
|
bitmap.draw(bitmapImage!, in: extent)
|
||
|
let scaledImage = bitmap.makeImage()
|
||
|
return UIImage.init(cgImage: scaledImage!)
|
||
|
}
|
||
|
|
||
|
}
|