WFCCImageMessageContent.m 2.3 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 "WFCCImageMessageContent.h"
  9. #import "WFCCNetworkService.h"
  10. #import "WFCCIMService.h"
  11. #import "WFCCUtilities.h"
  12. #import "Common.h"
  13. @implementation WFCCImageMessageContent
  14. + (instancetype)contentFrom:(UIImage *)image {
  15. WFCCImageMessageContent *content = [[WFCCImageMessageContent alloc] init];
  16. UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;
  17. NSString *path = [[WFCCUtilities getDocumentPathWithComponent:@"/IMG"] stringByAppendingPathComponent:[NSString stringWithFormat:@"img%lld.jpg", recordTime]];
  18. NSData *imgData = UIImageJPEGRepresentation([WFCCUtilities generateThumbnail:image withWidth:1024 withHeight:1024], 0.85);
  19. [imgData writeToFile:path atomically:YES];
  20. content.localPath = path;
  21. content.thumbnail = [WFCCUtilities generateThumbnail:image withWidth:120 withHeight:120];
  22. return content;
  23. }
  24. - (WFCCMessagePayload *)encode {
  25. WFCCMediaMessagePayload *payload = [[WFCCMediaMessagePayload alloc] init];
  26. payload.contentType = [self.class getContentType];
  27. payload.searchableContent = @"[图片]";
  28. payload.binaryContent = UIImageJPEGRepresentation(self.thumbnail, 0.45);
  29. payload.mediaType = Media_Type_IMAGE;
  30. payload.remoteMediaUrl = self.remoteUrl;
  31. payload.localMediaPath = self.localPath;
  32. return payload;
  33. }
  34. - (void)decode:(WFCCMessagePayload *)payload {
  35. if ([payload isKindOfClass:[WFCCMediaMessagePayload class]]) {
  36. WFCCMediaMessagePayload *mediaPayload = (WFCCMediaMessagePayload *)payload;
  37. self.thumbnail = [UIImage imageWithData:payload.binaryContent];
  38. self.remoteUrl = mediaPayload.remoteMediaUrl;
  39. self.localPath = mediaPayload.localMediaPath;
  40. }
  41. }
  42. - (UIImage *)thumbnail {
  43. if (!_thumbnail) {
  44. UIImage *image = [UIImage imageWithContentsOfFile:self.localPath];
  45. _thumbnail = [WFCCUtilities generateThumbnail:image withWidth:120 withHeight:120];
  46. }
  47. return _thumbnail;
  48. }
  49. + (int)getContentType {
  50. return MESSAGE_CONTENT_TYPE_IMAGE;
  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