WFCCStickerMessageContent.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // WFCCImageMessageContent.m
  3. // WFChatClient
  4. //
  5. // Created by heavyrain on 2017/9/2.
  6. // Copyright © 2017年 wildfire chat. All rights reserved.
  7. //
  8. #import "WFCCStickerMessageContent.h"
  9. #import "WFCCNetworkService.h"
  10. #import "WFCCIMService.h"
  11. #import "WFCCUtilities.h"
  12. #import "Common.h"
  13. @implementation WFCCStickerMessageContent
  14. + (instancetype)contentFrom:(NSString *)stickerPath {
  15. WFCCStickerMessageContent *content = [[WFCCStickerMessageContent alloc] init];
  16. content.localPath = stickerPath;
  17. content.size = [UIImage imageWithContentsOfFile:stickerPath].size;
  18. return content;
  19. }
  20. - (WFCCMessagePayload *)encode {
  21. WFCCMediaMessagePayload *payload = [[WFCCMediaMessagePayload alloc] init];
  22. payload.extra = self.extra;
  23. payload.contentType = [self.class getContentType];
  24. payload.searchableContent = @"[动态表情]";
  25. payload.mediaType = Media_Type_STICKER;
  26. payload.remoteMediaUrl = self.remoteUrl;
  27. payload.localMediaPath = self.localPath;
  28. NSMutableDictionary *dataDict = [NSMutableDictionary dictionary];
  29. [dataDict setObject:@(self.size.width) forKey:@"x"];
  30. [dataDict setObject:@(self.size.height) forKey:@"y"];
  31. payload.binaryContent = [NSJSONSerialization dataWithJSONObject:dataDict
  32. options:kNilOptions
  33. error:nil];
  34. return payload;
  35. }
  36. - (void)decode:(WFCCMessagePayload *)payload {
  37. [super decode:payload];
  38. if ([payload isKindOfClass:[WFCCMediaMessagePayload class]]) {
  39. WFCCMediaMessagePayload *mediaPayload = (WFCCMediaMessagePayload *)payload;
  40. self.remoteUrl = mediaPayload.remoteMediaUrl;
  41. self.localPath = mediaPayload.localMediaPath;
  42. }
  43. NSError *__error = nil;
  44. NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:payload.binaryContent
  45. options:kNilOptions
  46. error:&__error];
  47. if (!__error) {
  48. self.size = CGSizeMake([dictionary[@"x"] floatValue], [dictionary[@"x"] floatValue]);
  49. }
  50. }
  51. + (int)getContentType {
  52. return MESSAGE_CONTENT_TYPE_STICKER;
  53. }
  54. + (int)getContentFlags {
  55. return WFCCPersistFlag_PERSIST_AND_COUNT;
  56. }
  57. + (void)load {
  58. [[WFCCIMService sharedWFCIMService] registerMessageContent:self];
  59. }
  60. - (NSString *)digest:(WFCCMessage *)message {
  61. return @"[动态表情]";
  62. }
  63. @end