Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support scala image subtitle size #843

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Sources/KSPlayer/Subtitle/KSSubtitle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,12 @@ open class SubtitleModel: ObservableObject {
text.addAttributes([.font: SubtitleModel.textFont],
range: NSRange(location: 0, length: text.length))
}
// 如果存在图片字幕,通过 scale 调整图片大小
if let image = part.image {
let targetSize = CGSize(width: image.size.width * SubtitleModel.imageSubtitleScale,
height: image.size.height * SubtitleModel.imageSubtitleScale)
part.image = image.scaled(to: targetSize)
}
}
parts = newParts
return true
Expand Down Expand Up @@ -384,3 +390,19 @@ open class SubtitleModel: ObservableObject {
}
}
}

public extension UIImage {
/// 返回一个指定尺寸的缩放后的图片
func scaled(to size: CGSize) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, self.scale)
self.draw(in: CGRect(origin: .zero, size: size))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return scaledImage ?? self
}
}

public extension SubtitleModel {
/// 图片字幕缩放比例,默认1.0代表不缩放
static var imageSubtitleScale: CGFloat = 1.0
}
Loading