WFCCLocationMessageContent.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // WFCCTextMessageContent.m
  3. // WFChatClient
  4. //
  5. // Created by heavyrain on 2017/8/16.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCCLocationMessageContent.h"
  9. #import "WFCCIMService.h"
  10. #import "Common.h"
  11. #import "WFCCUtilities.h"
  12. @implementation WFCCLocationMessageContent
  13. - (WFCCMessagePayload *)encode {
  14. WFCCMessagePayload *payload = [[WFCCMessagePayload alloc] init];
  15. payload.contentType = [self.class getContentType];
  16. payload.searchableContent = self.title;
  17. payload.binaryContent = UIImageJPEGRepresentation(self.thumbnail, 0.67);
  18. NSMutableDictionary *dataDict = [NSMutableDictionary dictionary];
  19. [dataDict setObject:@(self.coordinate.latitude) forKey:@"lat"];
  20. [dataDict setObject:@(self.coordinate.longitude) forKey:@"long"];
  21. payload.content = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dataDict
  22. options:kNilOptions
  23. error:nil] encoding:NSUTF8StringEncoding];
  24. return payload;
  25. }
  26. - (void)decode:(WFCCMessagePayload *)payload {
  27. self.title = payload.searchableContent;
  28. self.thumbnail = [UIImage imageWithData:payload.binaryContent];
  29. NSError *__error = nil;
  30. NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[payload.content dataUsingEncoding:NSUTF8StringEncoding]
  31. options:kNilOptions
  32. error:&__error];
  33. if (!__error) {
  34. double latitude = [dictionary[@"lat"] doubleValue];
  35. double longitude = [dictionary[@"long"] doubleValue];
  36. self.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
  37. }
  38. }
  39. + (int)getContentType {
  40. return MESSAGE_CONTENT_TYPE_LOCATION;
  41. }
  42. + (int)getContentFlags {
  43. return WFCCPersistFlag_PERSIST_AND_COUNT;
  44. }
  45. + (instancetype)contentWith:(CLLocationCoordinate2D) coordinate title:(NSString *)title thumbnail:(UIImage *)thumbnail {
  46. WFCCLocationMessageContent *content = [[WFCCLocationMessageContent alloc] init];
  47. content.coordinate = coordinate;
  48. content.title = title;
  49. content.thumbnail = [WFCCUtilities generateThumbnail:thumbnail withWidth:180 withHeight:120];;
  50. return content;
  51. }
  52. + (void)load {
  53. [[WFCCIMService sharedWFCIMService] registerMessageContent:self];
  54. }
  55. - (NSString *)digest {
  56. return @"[位置]";
  57. }
  58. @end