如果有一天,你的努力配得上你的梦想,那么你的梦想也绝不会辜负你的努力。
目录
一、iOS -Base64字符与图片之间的转换
1.1图片转 base64字符串
1.2 base64转图片
二、保存截图
三、二维码生成
四、图片旋转
一、iOS -Base64字符与图片之间的转换
1.1 图片转 base64字符串
swift
//图片转 base 64
guard let image = UIImage(named: "图片名称") else {return}
//jpeg格式 compressionQuality: 压缩质量
guard let imageData = image.jpegData(compressionQuality: 1) else { return }
//png格式
guard let imageData2 = image.pngData() else { return }
let base64ImageStr = imageData.base64EncodedString(options: .lineLength64Characters)
oc
UIImage * image = [UIImage imageNamed:@"图片名称"];
NSData * imageData = UIImageJPEGRepresentation(image, 1);
// NSData * imageData = UIImagePNGRepresentation(image);
NSString * base64Str = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
1.2 base64转图片
swift
let base64ImageStr = "Base64字符串"
guard let showImageData = Data(base64Encoded: base64ImageStr, options: .ignoreUnknownCharacters) else {return}
imageView?.image = UIImage(data: showImageData )
oc
NSString * base64Str = @"图片base64字符串";
NSData * showData = [[NSData alloc]initWithBase64EncodedString:base64Str options:NSDataBase64DecodingIgnoreUnknownCharacters];
imageView.image = [UIImage imageWithData:showData];
备注:
有时候,后台返回的Base64字符是这种格式的:data:image/png;base64,iVBORw0K.............""
这个时候直接进行转换会失败,解决方法是去掉前缀:"data:image/png;base64,"
eg:
var base64ImageStr = ""
if let imageUrl = model?.imgUrl{
let arr = imageUrl.components(separatedBy: ",")
base64ImageStr = arr.last ?? ""
}
二、保存截图
@objc func saveScreenShot(){
guard let bgView = view.viewWithTag(100) else { return }
let tempImage = bgView.screenShot(size: bgView.size)
guard let haveImage = tempImage else {
showHudText("获取图片异常")
return
}
SPAlbumManager.checkPhotoAuthState { isAuth in
if isAuth {
SPAlbumManager.saveImageToAlbum(image: haveImage){ [weak self]finish in
self?.showHudText(finish ? "保存成功" : "保存失败")
}
}
}
}
三、二维码生成
func setQrCodeUI(){
let codeImageView: UIImageView = {
let imageView = UIImageView()
view.addSubview(imageView)
imageView.snp.makeConstraints{
$0.centerX.equalToSuperview()
$0.top.height.width.equalTo(200)
}
return imageView
}()
DispatchQueue.global(qos: .utility).async(execute: {
let image = UIImage.setupQRCodeImage("https://apphr4pyvl34424.h5.xiaoeknow.com/p/decorate/homepage")
DispatchQueue.main.async {
codeImageView.image = image
}
})
}
四、图片旋转
@objc func btnOnClick(btn:UIButton){
if btn.tag == 0 {
imgView.layer.add(rotationAnim, forKey: "kLightImage")
}else{
imgView.layer.removeAnimation(forKey: "kLightImage")
}
}
func imgViewRo(){
let stackView:UIStackView = UIStackView()
stackView.distribution = .fillEqually
stackView.alignment = .fill
stackView.axis = .horizontal
view.addSubview(stackView)
stackView.snp.makeConstraints{
$0.width.equalTo(200)
$0.top.equalTo(100)
$0.height.equalTo(48)
$0.centerX.equalToSuperview()
}
for (i, element) in ["开始","暂停"].enumerated() {
let btn = UIButton()
btn.setTitle(element, for: .normal)
btn.backgroundColor = UIColor.randomColor
btn.tag = i
btn.addTarget(self, action: #selector(btnOnClick(btn:)), for: .touchUpInside)
stackView.addArrangedSubview(btn)
}
imgView = UIImageView()
imgView.image = UIImage(named: "home_shop_pop_light")
view.addSubview(imgView)
imgView.snp.makeConstraints{
$0.centerY.centerX.equalToSuperview()
}
imgView.layer.add(rotationAnim, forKey: "kLightImage")
}
项目地址:项目下载链接
效果展示图.gif
网友评论