TUIGroupAvatar+Helper.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // TUIGroupAvatar+Helper.m
  3. // TIMCommon
  4. //
  5. // Created by wyl on 2023/4/27.
  6. // Copyright © 2023 Tencent. All rights reserved.
  7. //
  8. #import <TUICore/TUIConfig.h>
  9. #import "TUIGroupAvatar+Helper.h"
  10. @implementation TUIGroupAvatar (Helper)
  11. + (UIImage *)getNormalGroupCacheAvatar:(NSString *)groupID groupType:(NSString *)groupType {
  12. /**
  13. *
  14. * Setup default avatar
  15. */
  16. UIImage *avatarImage = nil;
  17. if (groupID.length > 0) {
  18. /**
  19. * If it is a group, change the group default avatar to the last used avatar
  20. */
  21. UIImage *avatar = nil;
  22. if (TUIConfig.defaultConfig.enableGroupGridAvatar) {
  23. NSString *key = [NSString stringWithFormat:@"TUIConversationLastGroupMember_%@", groupID];
  24. NSInteger member = [NSUserDefaults.standardUserDefaults integerForKey:key];
  25. avatar = [TUIGroupAvatar getCacheAvatarForGroup:groupID number:(UInt32)member];
  26. }
  27. avatarImage = avatar ? avatar : DefaultGroupAvatarImageByGroupType(groupType);
  28. ;
  29. return avatarImage;
  30. }
  31. return avatarImage;
  32. }
  33. + (void)configAvatarByParam:(NSDictionary *)param targetView:(UIImageView *)targetView {
  34. NSString *groupID = param[@"groupID"];
  35. NSString *faceUrl = param[@"faceUrl"];
  36. NSString *groupType = param[@"groupType"];
  37. UIImage *originAvatarImage = param[@"originAvatarImage"];
  38. if (groupID.length > 0) {
  39. /**
  40. *
  41. * Group avatar
  42. */
  43. if (IS_NOT_EMPTY_NSSTRING(faceUrl)) {
  44. /**
  45. *
  46. * The group avatar has been manually set externally
  47. */
  48. [targetView sd_setImageWithURL:[NSURL URLWithString:faceUrl] placeholderImage:originAvatarImage];
  49. } else {
  50. /**
  51. * The group avatar has not been set externally. If the synthetic avatar is allowed, the synthetic avatar will be used; otherwise, the default
  52. * avatar will be used.
  53. */
  54. if (TUIConfig.defaultConfig.enableGroupGridAvatar) {
  55. /**
  56. *
  57. * If the synthetic avatar is allowed, the synthetic avatar will be used
  58. * 1. Asynchronously obtain the cached synthetic avatar according to the number of group members
  59. * 2. If the cache is hit, use the cached synthetic avatar directly
  60. * 3. If the cache is not hit, recompose a new avatar
  61. *
  62. * Note:
  63. * 1. Since "asynchronously obtaining cached avatars" and "synthesizing avatars" take a long time, it is easy to cause cell reuse problems, so
  64. * it is necessary to confirm whether to assign values directly according to groupID.
  65. * 2. Use SDWebImage to implement placeholder, because SDWebImage has already dealt with the problem of cell reuse
  66. */
  67. // 1. Obtain group avatar from cache
  68. // fix: The getCacheGroupAvatar needs to request the
  69. // network. When the network is disconnected, since the headImageView is not set, the current conversation sends a message, the conversation is
  70. // moved up, and the avatar of the first conversation is reused, resulting in confusion of the avatar.
  71. [targetView sd_setImageWithURL:nil placeholderImage:originAvatarImage];
  72. [TUIGroupAvatar getCacheGroupAvatar:groupID
  73. callback:^(UIImage *avatar, NSString *groupID) {
  74. if ([groupID isEqualToString:groupID]) {
  75. // 1.1 When the callback is invoked, the cell is not reused
  76. if (avatar != nil) {
  77. // 2. Hit the cache and assign directly
  78. [targetView sd_setImageWithURL:nil placeholderImage:avatar];
  79. } else {
  80. // 3. Synthesize new avatars asynchronously without hitting cache
  81. [targetView sd_setImageWithURL:nil placeholderImage:originAvatarImage];
  82. [TUIGroupAvatar
  83. fetchGroupAvatars:groupID
  84. placeholder:originAvatarImage
  85. callback:^(BOOL success, UIImage *image, NSString *groupID) {
  86. if ([groupID isEqualToString:groupID]) {
  87. // When the callback is invoked, the cell is not reused
  88. [targetView
  89. sd_setImageWithURL:nil
  90. placeholderImage:success ? image : DefaultGroupAvatarImageByGroupType(groupType)];
  91. } else {
  92. // callback, When the callback is invoked, the cell has
  93. // been reused to other groupIDs. Since a new callback will be triggered when the new
  94. // groupID synthesizes new avatar, it is ignored here
  95. }
  96. }];
  97. }
  98. } else {
  99. // 1.2 callback ,cell groupID。 groupID
  100. // callback, 1.2 When the callback is invoked, the cell has been reused to other groupIDs. Since a new
  101. // callback will be triggered when the new groupID gets the cache, it is ignored here
  102. }
  103. }];
  104. } else {
  105. /**
  106. * Synthetic avatars are not allowed, use the default avatar directly
  107. */
  108. [targetView sd_setImageWithURL:nil placeholderImage:originAvatarImage];
  109. }
  110. }
  111. } else {
  112. /**
  113. * Personal avatar
  114. */
  115. [targetView sd_setImageWithURL:[NSURL URLWithString:faceUrl] placeholderImage:originAvatarImage];
  116. }
  117. }
  118. @end