WFCCStickerMessageContent.m 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.contentType = [self.class getContentType];
  23. payload.searchableContent = @"[动态表情]";
  24. payload.mediaType = Media_Type_File;
  25. payload.remoteMediaUrl = self.remoteUrl;
  26. payload.localMediaPath = self.localPath;
  27. NSMutableDictionary *dataDict = [NSMutableDictionary dictionary];
  28. [dataDict setObject:@(self.size.width) forKey:@"x"];
  29. [dataDict setObject:@(self.size.height) forKey:@"y"];
  30. payload.binaryContent = [NSJSONSerialization dataWithJSONObject:dataDict
  31. options:kNilOptions
  32. error:nil];
  33. return payload;
  34. }
  35. - (void)decode:(WFCCMessagePayload *)payload {
  36. if ([payload isKindOfClass:[WFCCMediaMessagePayload class]]) {
  37. WFCCMediaMessagePayload *mediaPayload = (WFCCMediaMessagePayload *)payload;
  38. self.remoteUrl = mediaPayload.remoteMediaUrl;
  39. self.localPath = mediaPayload.localMediaPath;
  40. }
  41. NSError *__error = nil;
  42. NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:payload.binaryContent
  43. options:kNilOptions
  44. error:&__error];
  45. if (!__error) {
  46. self.size = CGSizeMake([dictionary[@"x"] floatValue], [dictionary[@"x"] floatValue]);
  47. }
  48. }
  49. + (int)getContentType {
  50. return MESSAGE_CONTENT_TYPE_STICKER;
  51. }
  52. + (int)getContentFlags {
  53. return WFCCPersistFlag_PERSIST_AND_COUNT;
  54. }
  55. + (void)load {
  56. [[WFCCIMService sharedWFCIMService] registerMessageContent:self];
  57. }
  58. - (NSString *)digest {
  59. return @"[动态表情]";
  60. }
  61. @end