diff --git a/iAppStore/Models/AppDetail.swift b/iAppStore/Models/AppDetail.swift index 5d127c9..a4adf44 100644 --- a/iAppStore/Models/AppDetail.swift +++ b/iAppStore/Models/AppDetail.swift @@ -111,6 +111,20 @@ struct AppDetail: Codable { String(format: "%.1f", averageUserRating) } + /// 截图展示大小 + var screenShotSize: CGSize { + let width = 200.0 + let defaultSize = CGSize(width: width, height: width) + let url = screenshotUrls?.first ?? "" + let size = url.imageAppleSize() + guard size != CGSize.zero else { + return defaultSize + } + + let height = Double(size.height) / Double(size.width) * width + return CGSize(width: width, height: height); + } + static func getNewModel(_ artistId: String) -> AppDetail { return AppDetail(advisories: nil, appletvScreenshotUrls: nil, artistId: Int(artistId)!, artistName: "", artistViewUrl: nil, artworkUrl100: nil, artworkUrl512: "", artworkUrl60: "", averageUserRating: 0, averageUserRatingForCurrentVersion: 0, bundleId: "", contentAdvisoryRating: "", currency: "", currentVersionReleaseDate: "", description: "", features: [], fileSizeBytes: nil, formattedPrice: nil, genreIds: [], genres: [], ipadScreenshotUrls: [], isGameCenterEnabled: false, isVppDeviceBasedLicensingEnabled: false, kind: "", languageCodesISO2A: [], minimumOsVersion: "", price: 0, primaryGenreId: 0, primaryGenreName: "", releaseDate: "", releaseNotes: nil, screenshotUrls:nil, sellerName: "", sellerUrl: nil, supportedDevices: [], trackCensoredName: "", trackContentRating: "", trackId: 0, trackName: "", trackViewUrl: "", userRatingCount: 0, userRatingCountForCurrentVersion: 0, version: "", wrapperType: "") } diff --git a/iAppStore/Shared/extensions/Strings.swift b/iAppStore/Shared/extensions/Strings.swift index 471996f..dcfda53 100644 --- a/iAppStore/Shared/extensions/Strings.swift +++ b/iAppStore/Shared/extensions/Strings.swift @@ -12,29 +12,42 @@ import CryptoKit extension String { - - /// 处理苹果图片尺寸 + /// 获取苹果缩略图尺寸 /// 类似: https://is1-ssl.mzstatic.com/image/thumb/PurpleSource124/v4/16/0a/18/160a1889-5d30-9ef1-09fd-cacd97f5f0bd/96e99d1b-6a47-4c78-a570-4b7682c5c9df_img5_5_0_zh-Hans.png/392x696bb.png - /// - Parameter scale: 缩放比例 - /// - Returns: 缩放后的 url - public func imageAppleScale(_ scale: Double = UIScreen.main.scale) -> String { + /// - Returns: 缩略图 Size + public func imageAppleSize() -> CGSize { if let url = URL(string: self) { let component = url.lastPathComponent let splits = component.components(separatedBy: CharacterSet.decimalDigits.inverted) if splits.count > 1, let fSize = splits.first, let lSize = splits.dropFirst().first { if let fdSize = Double(fSize), let ldSize = Double(lSize) { - let newfSize = String(Int(fdSize * scale)) - let newlSize = String(Int(ldSize * scale)) - let newComponent = component - .replacingOccurrences(of: fSize, with: newfSize) - .replacingOccurrences(of: lSize, with: newlSize) - let newUrl = url.deletingLastPathComponent().appendingPathComponent(newComponent).relativeString - return newUrl + let size = CGSize(width: fdSize, height: ldSize) + return size } } } + return CGSize.zero + } + + /// 处理苹果图片尺寸 + /// 类似: https://is1-ssl.mzstatic.com/image/thumb/PurpleSource124/v4/16/0a/18/160a1889-5d30-9ef1-09fd-cacd97f5f0bd/96e99d1b-6a47-4c78-a570-4b7682c5c9df_img5_5_0_zh-Hans.png/392x696bb.png + /// - Parameter scale: 缩放比例 + /// - Returns: 缩放后的 url + public func imageAppleScale(_ scale: Double = UIScreen.main.scale) -> String { + if let url = URL(string: self) { + let component = url.lastPathComponent + let size = imageAppleSize() + let newfSize = String(Int(size.width * scale)) + let newlSize = String(Int(size.height * scale)) + let newComponent = component + .replacingOccurrences(of: String(Int(size.width)), with: newfSize) + .replacingOccurrences(of: String(Int(size.height)), with: newlSize) + let newUrl = url.deletingLastPathComponent().appendingPathComponent(newComponent).relativeString + return newUrl + } + return self } diff --git a/iAppStore/components/rankLists/AppDetailContentView.swift b/iAppStore/components/rankLists/AppDetailContentView.swift index 2f281bd..e3fda84 100644 --- a/iAppStore/components/rankLists/AppDetailContentView.swift +++ b/iAppStore/components/rankLists/AppDetailContentView.swift @@ -184,7 +184,7 @@ struct AppDetailScreenShowView: View { if appModel.app != nil && appModel.app!.isSupportiPhone { - AppDetailScreenShotView(screenshotUrls: appModel.app?.screenshotUrls, imageWidth: 200) + AppDetailScreenShotView(screenshotUrls: appModel.app?.screenshotUrls, imageSize: appModel.app?.screenShotSize) HStack { Image(systemName: "iphone").foregroundColor(.gray).font(.body) @@ -213,7 +213,7 @@ struct AppDetailScreenShowView: View { if (appModel.app != nil && extendiPadShot) || (appModel.app != nil && !appModel.app!.isSupportiPhone && appModel.app!.isSupportiPad) { - AppDetailScreenShotView(screenshotUrls: appModel.app?.ipadScreenshotUrls, imageWidth: 300) + AppDetailScreenShotView(screenshotUrls: appModel.app?.ipadScreenshotUrls, imageSize: appModel.app?.screenShotSize) HStack { Image(systemName: "ipad").foregroundColor(.gray).font(.body) @@ -234,7 +234,7 @@ struct AppDetailScreenShowView: View { struct AppDetailScreenShotView: View { var screenshotUrls: [String]? - var imageWidth: CGFloat = 200 + var imageSize: CGSize? @State private var selectedShot: Bool = false @State private var selectedImgUrl: String? @@ -255,14 +255,17 @@ struct AppDetailScreenShotView: View { .resizable() .scaledToFit() .cornerRadius(11) - .frame(width: imageWidth) + .frame(width: imageSize?.width) }, image: { $0.resizable() .scaledToFit() .cornerRadius(11) - .overlay(RoundedRectangle(cornerRadius: 11).stroke(Color.gray, lineWidth: 0.1)) - .frame(width: imageWidth) + .overlay(RoundedRectangle(cornerRadius: 11) + .stroke(Color.gray, lineWidth: 0.1) + .frame(width: imageSize?.width, height: imageSize?.height)) + .frame(width: imageSize?.width, height: imageSize?.height) + } ) }