Commit 6e258771 authored by Steven杜宇's avatar Steven杜宇

// AI

parent 116605e5
......@@ -8,6 +8,8 @@
import UIKit
import SDWebImage
import Photos
import PhotosUI
class YHAIPictureMessageCell: UITableViewCell {
......@@ -68,16 +70,6 @@ class YHAIPictureMessageCell: UITableViewCell {
ratio = img.size.width/img.size.height
}
imgH = imgW/ratio
// if imgH > 476 {
// imgH = 476
// imgW = imgH*ratio
//
// } else {
// imgW = 220
// imgH = imgW/ratio
// }
return CGSizeMake(imgW, imgH)
}
......@@ -86,10 +78,17 @@ class YHAIPictureMessageCell: UITableViewCell {
return v
}()
lazy var downloadBtn: UIButton = {
let btn = UIButton()
btn.setImage(UIImage(named: "ai_chat_img_download"), for: .normal)
btn.addTarget(self, action: #selector(didDownloadBtnClicked), for: .touchUpInside)
btn.layer.cornerRadius = 6.0
btn.YH_clickEdgeInsets = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
return btn
}()
lazy var whiteContentView: UIView = {
let v = UIView()
v.layer.cornerRadius = 12.0
v.clipsToBounds = true
let tap = UITapGestureRecognizer(target: self, action: #selector(didMessageClicked))
v.addGestureRecognizer(tap)
return v
......@@ -109,6 +108,11 @@ class YHAIPictureMessageCell: UITableViewCell {
setupUI()
}
override func layoutSubviews() {
super.layoutSubviews()
roundCorners(keepTopLeft: true)
}
func setupUI() {
selectionStyle = .none
......@@ -117,6 +121,7 @@ class YHAIPictureMessageCell: UITableViewCell {
contentView.addSubview(shadowView)
contentView.addSubview(whiteContentView)
whiteContentView.addSubview(imgView)
whiteContentView.addSubview(downloadBtn)
whiteContentView.snp.makeConstraints { make in
make.left.equalTo(20)
......@@ -130,6 +135,13 @@ class YHAIPictureMessageCell: UITableViewCell {
make.edges.equalToSuperview()
}
downloadBtn.snp.makeConstraints { make in
make.width.equalTo(26)
make.height.equalTo(26)
make.left.equalTo(16)
make.bottom.equalTo(-16)
}
shadowView.snp.makeConstraints { make in
make.edges.equalTo(whiteContentView)
}
......@@ -139,6 +151,98 @@ class YHAIPictureMessageCell: UITableViewCell {
UIApplication.shared.yhKeyWindow()?.endEditing(true)
YHPictureReviewManager.shared.showNetWorkPicturs(curIndex: 0, arrPicturs: [imgInfo.imageUrl])
}
@objc func didDownloadBtnClicked() {
if imgInfo.imageType == YHAIImageType.url.rawValue {
// 尝试从缓存中获取图片
let cachedImage = SDImageCache.shared.imageFromCache(forKey: imgInfo.imageUrl)
downloadImage(cachedImage, urlString: imgInfo.imageDownloadUrl)
} else if imgInfo.imageType == YHAIImageType.local.rawValue {
let img = UIImage(named: imgInfo.localImageName)
downloadImage(img, urlString: imgInfo.imageDownloadUrl)
}
}
func downloadImage(_ img: UIImage?, urlString: String) {
if let img = img {
saveImage(img)
return
}
guard let url = URL(string: urlString) else {
YHHUD.flash(message: "保存失败")
return
}
let task = URLSession.shared.dataTask(with: url) {
[weak self] data, response, error in
DispatchQueue.main.async {
guard let self = self else { return }
guard let data = data else {
YHHUD.flash(message: "保存失败")
return
}
let image = UIImage(data: data)
if let image = image {
self.saveImage(image)
}
}
}
task.resume()
}
func saveImage(_ image: UIImage) {
// 确保应用有权访问相册
PHPhotoLibrary.requestAuthorization { status in
if status == .authorized {
// 保存图片到相册
DispatchQueue.main.async {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
YHHUD.flash(message: "保存成功")
}
} else {
DispatchQueue.main.async {
YHHUD.flash(message: "保存失败,请检查系统权限")
}
}
}
}
func roundCorners(keepTopLeft: Bool) {
let path = UIBezierPath()
let radius: CGFloat = 6.0 // 圆角半径
// 设置路径
path.move(to: CGPoint(x: keepTopLeft ? 0 : radius, y: 0)) // 从左上角开始,视情况决定是否切圆角
// 左上角
if keepTopLeft {
path.addLine(to: CGPoint(x: 0, y: 0)) // 不切左上角
} else {
path.addArc(withCenter: CGPoint(x: radius, y: radius), radius: radius, startAngle: .pi, endAngle: .pi * 1.5, clockwise: true)
}
// 右上角
path.addLine(to: CGPoint(x: self.bounds.width, y: 0))
path.addArc(withCenter: CGPoint(x: self.bounds.width - radius, y: radius), radius: radius, startAngle: .pi * 1.5, endAngle: 0, clockwise: true)
// 右下角
path.addLine(to: CGPoint(x: self.bounds.width, y: self.bounds.height))
path.addArc(withCenter: CGPoint(x: self.bounds.width - radius, y: self.bounds.height - radius), radius: radius, startAngle: 0, endAngle: .pi / 2, clockwise: true)
// 左下角
path.addLine(to: CGPoint(x: 0, y: self.bounds.height))
path.addArc(withCenter: CGPoint(x: radius, y: self.bounds.height - radius), radius: radius, startAngle: .pi / 2, endAngle: .pi, clockwise: true)
path.close() // 关闭路径
// 创建 CAShapeLayer
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
self.whiteContentView.layer.mask = maskLayer // 设置 UIView 的 mask
}
}
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Group 2033196882@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Group 2033196882@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment