MapAmapMarkerPictureView.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // MapAmapMarkerPictureView.swift
  3. // map_amap_ios
  4. //
  5. // Created by 诺诺诺的言 on 2025/7/22.
  6. //
  7. import Foundation
  8. class MapAmapMarkerPictureView: UIView {
  9. let markerType: any MapMarkerSupportType
  10. var isSelected: Bool = false {
  11. didSet {
  12. if oldValue != isSelected {
  13. updateImage()
  14. }
  15. }
  16. }
  17. private let imageView = UIImageView()
  18. init(markerType: any MapMarkerSupportType) {
  19. self.markerType = markerType
  20. super.init(frame: .zero)
  21. setupImageView()
  22. }
  23. required init?(coder: NSCoder) {
  24. fatalError("init(coder:) has not been implemented")
  25. }
  26. private func setupImageView() {
  27. imageView.contentMode = .scaleAspectFit
  28. imageView.translatesAutoresizingMaskIntoConstraints = false
  29. addSubview(imageView)
  30. NSLayoutConstraint.activate([
  31. imageView.topAnchor.constraint(equalTo: topAnchor),
  32. imageView.bottomAnchor.constraint(equalTo: bottomAnchor),
  33. imageView.leadingAnchor.constraint(equalTo: leadingAnchor),
  34. imageView.trailingAnchor.constraint(equalTo: trailingAnchor)
  35. ])
  36. updateImage()
  37. }
  38. private func updateImage() {
  39. imageView.image = uiImage
  40. }
  41. private var uiImage: UIImage? {
  42. let imageName = markerType.imageName(selected: isSelected)
  43. if let image = UIImage(named: imageName) {
  44. return image
  45. }
  46. // 尝试从插件资源束加载
  47. let bundleURL = Bundle(for: MapAmapMarkerNormalView.self).url(forResource: "map_amap_ios_image_source", withExtension: "bundle")
  48. if let bundleURL = bundleURL, let resourceBundle = Bundle(url: bundleURL) {
  49. return UIImage(named: imageName, in: resourceBundle, compatibleWith: nil)
  50. }
  51. return nil
  52. }
  53. ///加载网络图片
  54. public func loadNetworkImage(imageUrl : String) {
  55. // 异步加载图片
  56. MapAmapMarkPictureLoadMananger.shared.loadAvatarAsync(
  57. from: URL(string: imageUrl)!,
  58. style: .circle
  59. ) { image, error in
  60. if let image = image {
  61. self.imageView.image = image
  62. }
  63. }
  64. }
  65. }